Saturday, May 20, 2017

windows - How to prevent opening same program on terminal server by the "same user" with command prompt?


I have several users that use Remote Desktop App (Server 2008) and when they open the .BAT file I have created, it opens up two programs...however, I need to prevent the same program (in this case Outlook) from opening up multiple times...


I created this line in my .BAT file...


tasklist /nh /fi "imagename eq outlook.exe" | find /i "Outlook.exe" > nul ||(start /min Outlook.exe)

It works fine...except, I have multiple users logging on to this "terminal server" and running the same .BAT file...so what happens is when "User 2" logs on, they don't get Outlook started because it already shows as running in the task mananger under "USER 1"...


I need to be able to distinguish the "imagename" and "username"...well the "username" would be %USERPROFILE%


Answer




How to prevent opening same program on terminal server by the “same user” with command prompt?


I have multiple users logging on to this "terminal server" and running
the same .BAT file...so what happens is when "User 2" logs on, they
don't get Outlook started because it already shows as running in the
task manager under "USER 1"



Give the below logic a shot as it'll use the %Username% environmental variable to only get the current TASKLIST entry for the Username of the user account profile that runs that command. . .


TASKLIST COMMANDS TO GET PROCESS FOR ONLY THE USER RUNNING IT


TASKLIST /NH /FI "USERNAME eq %Username%" /FI "imagename eq outlook.exe"

USING WITH YOUR EXAMPLE LOGIC


TASKLIST /NH /FI "USERNAME eq %Username%" /FI "imagename eq outlook.exe" | find /i "Outlook.exe" > nul ||(start /min Outlook.exe)



Additional Reading and Resources


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...