Thursday, May 23, 2013

Powershell Remote User Logoff


When patching servers, any disconnected user sessions typically prevent the post-patch auto reboot. To remedy this you could use the following Powershell script :

foreach ($_ in get-content servers.txt) {(gwmi win32_operatingsystem -ComputerName$_).Win32Shutdown(4)}

The script uses an input file named servers.txt populated with the names of all the servers targeted by auto-patching GPOs to force a logoff of all users on each server.

The Win32Shutdown(4) is a value that can be changed for different purposes, so if you want to use a similar script from a command line you can use the following values :
0 – logoff
4 – forced logoff
1 – shutdown
5 – forced shutdown
2 – reboot
6 – forced reboot
8 – power off
12 – forced power off