'* Script Name: ShutDownIfNotLoggedOn - Local.vbs '* Created On: 12/13/2007 '* Author: Michael C. Panagos '* Website: http://www.grimadmin.com '* Purpose: Script to get current user logged on and perform shutdown on computer if no one is logged on '* History: Michael C. Panagos 12/13/2007 '* Initial Draft. '* Legal: Copying and distribution of this code, with or without modification, '* are permitted in any medium without royalty provided the copyright '* notice and this notice are preserved. This code is offered as-is, '* without any warranty. 'Account used to run script needs WMI permissions to shut down computer. 'Set error capture On Error Resume Next 'Get local computer name Set wshNetwork = WScript.CreateObject( "WScript.Network" ) strComputerName = wshNetwork.ComputerName 'strComputerName = "ComputerABC" 'For testing specific computer 'Setup WMI calling for logon name and shutdown assignment Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2") 'Get currently logged on user's username Set colComputer = objWMIService.ExecQuery _ ("Select * from Win32_ComputerSystem") For Each objComputer in colComputer If IsNull(objComputer.UserName) Then 'If no one is logged on strLoggedOn = "No" Else strLoggedOn = "Yes" End If Next 'If no one is logged on shut down the computer If strLoggedOn = "No" Then Set colComputer = objWMIService.ExecQuery _ ("Select * from Win32_OperatingSystem") For Each objComputer in colComputer objComputer.Win32Shutdown(1) Next End If