I want to modify the script to launch the calculator app for 2 minutes then close it. The script can launch the app only. Please help.
Set colMonitoredEvents = GetObject("winmgmts:")._
ExecNotificationQuery("SELECT * FROM Win32_PowerManagementEvent")
Do
Set strLatestEvent = colMonitoredEvents.NextEvent
If strLatestEvent.EventType = 4 Then
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
If objItem.name = "Calculator.exe" then objItem.terminate
Next
ElseIf strLatestEvent.EventType = 7 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc.exe", 1, false
End If
Loop
I want to modify it to launch and close the calculator.exe.
This is accomplished simply with Sleep
and TaskKill
. Note that, starting probably with Windows 8, the calculator is a UWP app and Calc.exe
is used to launch it, but Calculator.exe
is the process name to kill it. On Windows 7, it would be Calc.exe
for both.
Set oWSH = CreateObject("Wscript.Shell")
oWSH.Run "Calc.exe", 1, False
WScript.Sleep 120000 'two minutes
oWSH.Run "TaskKill /im Calculator.exe /f", 0, False
If you want to retain the capability of the original script, which kills calculator when the computer goes to sleep and restarts it on wake, and add a two minute delay before the kill, that would be done like this:
Set oWSH = CreateObject("Wscript.Shell")
Set colMonitoredEvents = GetObject("winmgmts:").ExecNotificationQuery("SELECT * FROM Win32_PowerManagementEvent")
Do
Set strLatestEvent = colMonitoredEvents.NextEvent
If strLatestEvent.EventType = 4 Then
WScript.Sleep 120000 'two minutes
oWSH.Run "TaskKill /im Calculator.exe /f", 0, False
ElseIf strLatestEvent.EventType = 7 Then
oWSH.Run "calc.exe", 1, False
End If
WScript.Sleep 500
Loop
Note that I added a .5 second delay in the loop to prevent unnecessary CPU load.