Conditional shutdown with a VBS script

by maistora
by maistora

9
I used to leave my computer at home running after I’ve leaft for work for various reasons. But wanted it to shutdown at a certain time. To do this I wrote a simple VB Script and used Windows Scheduled Tasks to run it.  This worked great, unless I happened to be at home and using the computer during that time. Then it would shut down anyway!
So I added in a check to see if certain programs were running, not to shut down.
Below is the modified script that will check if World of Warcraft (wow.exe) or iTunes (itunes.exe) is running. If they are, then it quites, other wise it will shutdown the computer.

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objWMIService2 = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\.\root\cimv2")
Set objShell = CreateObject("WScript.Shell")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'iTunes.exe' or Name = 'Wow.exe'")
Set colOperatingSystems = objWMIService2.ExecQuery ("Select * from Win32_OperatingSystem")
If colProcessList.Count > 0 Then
 wscript.quit()
Else
 For Each objOperatingSystem in colOperatingSystems
 objOperatingSystem.Win32Shutdown(1)
 Next
End If

To specify a program to check for, put its filename in the colProcessList line where itunes.exe and wow.exe are now. To check the filename you need to check for you simple need to start it, then open Windows Task Manager and find how it is listed there.

Leave a Reply

Your email address will not be published. Required fields are marked *