1

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)?

1 Answers1

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
  • 854