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.

030
  • 6,085

2 Answers2

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/

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.