1

There is an application in a multi-user environment that can only be open in one session on a workstation. The users move around frequently from PC to PC, leaving the application open, and the workstation locked. Disabling user switching is determinant to productivity. I need the system to force logoff any user besides the Consul session. The session ID is a variable that cannot be predicted. This is what I got so far...

@ echo off

query session > c:\users\session.txt

powershell -Command "Get-Content c:\users\session.txt | Where-Object {$_ -notmatch 'console'} | Set-Content c:\users\session2.txt"

for /f "skip=2 tokens=2," %i in (c:\users\session2.txt) DO C:\Windows\System32\logoff.exe %i

The goal is to put this into a .bat to run at user logon so whoever is ACTUALLY using the computer can use the applicaton. Dataloss of the inactive session is no concern. Ideally, rdp-tcp and services would stay active sessions as well.

2 Answers 2

0

Well, this isn't quite what you're asking for but it's close: a means to have users logged off automatically when they're classed as idle:

https://4sysops.com/archives/automatically-log-off-idle-users-in-windows/

2
  • 1
    Welcome to ServerFault. While the link you provided may answer the question, your answer will become useless if the linked page ever goes away. Please also include at least a summary of the solution here. Jul 1, 2015 at 14:50
  • Good point, however as the solution is rather involved and depends on a the download of a little (free) utility, probably it's best just to provide a secondary link to much the same stuff: intelliadmin.com/index.php/2011/11/…
    – Legacoid
    Jul 2, 2015 at 12:06
0

Easier approach would be to simply kill all instances of the application that are NOT running in the active session.

Looks like you've got a good start on getting session information from PowerShell ... you can also use the qwinsta command to get all Session IDs. From there, parse the list to only get the session IDs tagged "active"

From there, you can use the taskkill command with a filter excluding the active Session ID, but killing the process every where else:

taskkill /f /fi "SESSION ne 1" /im apptokill.exe

where 1 above was the Session ID of the active session. The command above will terminate all instances of apptokill.exe NOT running in Session ID 1.

4
  • Two thoughts: With query session the Session ID is an unknown variable. With taskkill user switching involves either unlocking the desktop or logging on. I do not want to kill the task if the user who has locked the desktop with the app running is unlocking - meaning that the last person logged on, when unlocking the screen, should have no change to processes or sessions.
    – user229603
    Jul 9, 2015 at 15:56
  • The query session generates the print out of sessions, followed by the get-content command in PS removes the line containing the console, and the id of that session along with it. This takes the guess work out of the IDs for ... logoff. I think the problem is in my for command syntax, unless my logic is flawed more in whole.
    – user229603
    Jul 9, 2015 at 16:16
  • That works! Thanks!!! @jimmiller Jim, how does the "SESSION ne 1" argument work? For the test when I had my test user log in after me qwinstat gave them an ID of 2. I switched back to my session qwinstat labed as 1 and ran your syntax to kill there eaglesoft.exe.
    – user229603
    Jul 9, 2015 at 19:48
  • I just tried this on the domain in question. The users are local admins. I switched users and could see the program to kill running in task manager with the inactive users name next to it. then in the command prompt I put taskkill /f /fi "SESSION ne 1" /im eaglesoft.exe
    – user229603
    Jul 9, 2015 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .