I needed a script to logoff all running Terminal Server sessions in order to rollout an install package. As you might know there is a commandline tool to logoff a session, it’s called logoff.exe.
These are the commandline options:
LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V]
sessionname The name of the session.
sessionid The ID of the session.
/SERVER:servername Specifies the Terminal server containing the user
session to log off (default is current).
/V Displays information about the actions performed.
No option to logoff all sessions is there?
On a Terminal Server there is a special session called the Listener session, you can see it with TSAdmin in the sessions tab:
A Listener is associated with a protocol (Microsoft RDP by default) and is used to setup new sessions. If you logoff a Listener session it will logoff all session that were created through it. Great, just what we need!
So Logoff 65536 will do the trick? Let’s try:
So Logoff is smart enough to ask for confirmation, we can prevent this by using the following commandline:
Echo Y ! Logoff 65536
Remko,
You might also want to checkout TekStop from TekWorkshop (Alex Danilychev). It allows you to logoff all users (via RDP or ICA) except for a specific user. (i.e. you can use it to logoff everyone except yourself).
Shawn
Shawn, thanks for that (didn't know). I will have a look...
As the session ID will grow, a better method would be to use the Session name for the Listener (RDP-Tcp or ICA-Tcp).
THis is easily done in vbscript:
Set objShell = CreateObject("WScript.Shell")
Set objProc = objShell.Exec("C:\WINDOWS\system32\logoff.exe RDP-Tcp /SERVER:NameOfTSServer")
objProc.StdIn.WriteLine "Y"
Do While objProc.Status = 0
WScript.Sleep 100
Loop
If you wanted to log off ALL user sessions, and not just the listener...
...use a "*" in the command instead of the listener's ID#. For example:
echo Y | logoff *
this can also be modified in your VBscript above. I would prefer not trying to execute a remote command through vbscript, just use WMI instead... for example:
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strLine & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Win32Shutdown(4)
Next
Using the Win32Shutdown class option has many variables that can be passed to it, including forced shutdowns, etc. #4 is a forced logoff event. Modify that script section to whatever suits your desires :)
this above also needs a definition for the variable "strLine". it's either the IP address or the hostname of the server in question. This can also be executed from wherever you have the permissions/rights to do so...