Shutdown (reboot) Windows workstation only if no one is logged in

over 10 years ago
I had a desire to shutdown workstations automatically, but only if no user was logged on. There isn't a current ZENworks Requirement that is "User logged on" there is one regarding primary user, but I wanted to avoid shutdown regardless of logged on user.

I created a bundle that copies/installs the following script onto the workstation, and then runs the script (and passes any reason text as desired). I had to copy/install the script to the workstation because there is still a flaw in the ZCM script translator that doesn't handle parts of the script properly if it was defined in the bundle action.
set ucount=1
set reason=%1
if %reason%.==. set reason=None
for /f "tokens=*" %%G in ('wmic ComputerSystem Get UserName ^| Find /c "\"') do (set ucount==%%G)
if %ucount% == 0 shutdown /s /t 5 /c %reason%

Should work for Windows XP and higher.



How To-Best Practice
Comment List
Related Discussions