Soldato
- Joined
- 7 Feb 2004
- Posts
- 8,215
- Location
- North East
Hello all, how would I schedule a task to close a program on sleep and open it on wake? The PC in question is running server 2008.
Only thing I can think of is tying in a task to run when a certain event is logged in eventviewer, not sure of any other way to do it. I haven't really used scheduled tasks on server 2008 though, it might be worth having a poke about.
Option Explicit
Dim wmiPowerManagementEvent, objProcess
Const cntEventEnteringSuspend = 4
Set wmiPowerManagementEvent = GetObject("winmgmts:").ExecNotificationQuery("Select * from Win32_PowerManagementEvent")
Do
If wmiPowerManagementEvent.NextEvent.EventType = cntEventEnteringSuspend Then
For Each objProcess in GetObject("winmgmts:").ExecQuery("select * from Win32_Process where name='ehshell.exe'")
objProcess.Terminate(0)
Next
End If
Loop
If you find Event ID 42 from Kernel-Power [Sleep] and Event ID 1 from Power-Troubleshooter [Wake], you can create a Scheduled Task that runs when those events are logged. In the Event Viewer, clicking an entry should provide an option on the right to Attach Task to this Event.
Might be easier than trying to get the script to work when you don't know what you're changing.![]()