在 Windows 10 笔记本电脑上打开盖子时执行脚本/程序

在 Windows 10 笔记本电脑上打开盖子时执行脚本/程序

当盖子打开时,有没有办法在 Windows 10 笔记本电脑上执行脚本或程序(假设屏幕未锁定且用户已登录)?

答案1

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

值 含义
4 进入挂起状态
7 从挂起状态恢复
10 电源状态改变
11 OEM 事件
18 自动恢复

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-powermanagementevent

相关内容