title: Batch - Get a list of users on a specific machine with a specific program open?
body: <p>A bit of background:</p>
  
  <p>At my company, we have a primitive application (that can only be opened from a few computers on our network) which monitors and allows our custom executables to be stopped, started, taken out of watchdog control, etc.</p>
  
  <p>I have developed a solution which triggers a batch file to run whenever a process times out or is restarted. This batch script is very simple, all it currently does is place an entry into the Windows event log telling us the name of the process that restarted.</p>
  
  <p>The first argument is the type of event and the second argument is the process name.</p>
  
  <p>See below:</p>
  
  <pre><code>if "%1%" == "restart" (
      eventcreate /L Application /SO Watchdog /T Warning /id 218 /D "Process %2 restarted!"
  ) else if "%1%" == "timeout" (
      eventcreate /L Application /SO Watchdog /T Warning /id 218 /D "Process %2 timed out!"
  )
  </code></pre>
  
  <p>This works great, but unfortunately this primitive application has no way of indicating (if a user manually restarted a process instead of the watchdog restarting it automatically) the name of the user who restarted the process.</p>
  
  <p>So my idea is to add some lines to this batch script get a list of all users on all machines (there are only a few possible machines) who have this primitive application open at the time. Since this application is rarely left open, it is probable that this will narrow it down to only one user, but even if multiple users are returned, I at least know it was one of them, rather than having no idea.</p>
  
  <p>So I desire something like this:</p>
  
  <pre><code>if "%1%" == "restart" (
      eventcreate /L Application /SO Watchdog /T Warning /id 218 /D "Process %2 restarted!"
  ) else if "%1%" == "timeout" (
      eventcreate /L Application /SO Watchdog /T Warning /id 218 /D "Process %2 timed out!"
  )
  
  REM Code to find all users on all machines who have this application open
  
  eventcreate /L Application /SO Watchdog /T Information /id 219 /D "&lt;list of users&gt;"
  </code></pre>
  
  <p>I have read a few questions on SO and I found this command which lists all users for a particular server:</p>
  
  <pre><code>qwinsta /server:&lt;server name&gt;
  </code></pre>
  
  <p>And this command will get a list of all instances of an application open on the current server:</p>
  
  <pre><code>tasklist /fi "imagename eq &lt;exe name&gt;.exe"
  </code></pre>
  
  <p>But I'm having difficulty relating these two commands to get a full list of users on selected machines with this particular application open.</p>