Is there any way to execute a script or a program on a Windows 10 laptop when the lid is opened (assuming the screen is not locked and a user logged in)?
Asked
Active
Viewed 566 times
1
-
Maybe in task manager using a trigger, not sure. – Moab Feb 18 '20 at 15:12
-
Similar question>>>>https://superuser.com/questions/146520/is-there-any-way-to-execute-something-when-closing-the-laptops-lid?rq=1 – Moab Feb 18 '20 at 15:14
-
Thanks for the info! I read this question as well, but I suppose this wouldn't work, since it's based on that the laptop will enter "sleep mode" as soon as the lid is closed, which then will trigger an event and so on. There are no options to set what to do for when opening the lid in the power options as well. Using a trigger in the event scheduler might work, but I wouldn't know which event gets triggered when the lid gets opened. – solarisISBC Feb 18 '20 at 15:44
-
There has to be a way, just don't know what it is, maybe someone will have a solution. – Moab Feb 18 '20 at 15:47
1 Answers
0
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
wscript.sleep 2000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc.exe", 1, false
End If
Loop
Value Meaning
4 Entering Suspend
7 Resume from Suspend
10 Power Status Change
11 OEM Event
18 Resume Automatic
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-powermanagementevent
Mark
- 706
- 4
- 3