gpt4 book ai didi

windows - 记录电源/电池事件

转载 作者:可可西里 更新时间:2023-11-01 10:37:15 25 4
gpt4 key购买 nike

刚买了一个 APC 备用电池并将 USB 数据线连接到我的 Windows 7 电脑上。它自动安装了一个驱动程序,现在在 Windows 托盘中我看到一个带有电源插头图标的电池,它显示了充电百分比。当我从墙上拔下 UPS 时,台式机进入电池模式并且小图标发生变化……就像笔记本电脑一样。

我想做的是在此事件发生时运行一个任务。不幸的是,电源状态的变化不会在事件查看器中记录事件,因此我可以将任务附加到它。显然有什么事情正在发生,因为图标在变化。当电源状态更改为电池时,如何记录事件?

谢谢,广告

最佳答案

执行此操作的一种方法是使用 WMI 通知一直运行的脚本。下面是一个脚本,用于监视 Win32_Battery WMI 类中对象的更改并报告所做的更改:

strComputer = "."

' Connect to WMI
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Create the Sink object used for the asynchronous notification.
Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")

' Send the query asynchronously. The SINK_OnObjectReady subroutine will be
' called if any data comes in.
objWMIService.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'Win32_Battery'"

while 1
Wscript.Sleep(1000)
Wend

Sub SINK_OnCompleted(iHResult, objErrorObject, objAsyncContext)
WScript.Echo "Asynchronous operation is done."
End Sub

Dim intLastLevel
intLastLevel = 0

Sub SINK_OnObjectReady(objObject, objAsyncContext)
' This runs whenever a change is made to the Win32_Battery object for
' your computer's battery. For more useful properties of this class,
' see: http://msdn.microsoft.com/en-us/library/aa394074(v=vs.85).aspx
Set objEvent=objObject.TargetInstance

Select Case objEvent.BatteryStatus
Case 1
if intLastLevel <> objEvent.EstimatedChargeRemaining Then
Wscript.Echo "Battery is discharging."
Wscript.Echo "Battery has", objEvent.EstimatedChargeRemaining + 1, "% left on battery."
Wscript.Echo "Battery has", objEvent.EstimatedRunTime, " minutes left on battery."
intLastLevel = objEvent.EstimatedChargeRemaining
End If
Case 2
Wscript.Echo "Battery is connected to AC."
Case 3
Wscript.Echo "Battery is fully charged."
Case 4
Wscript.Echo "Battery is currently low."
Case 5
Wscript.Echo "Battery is currently critically low."
Case 6
Wscript.Echo "Battery is currrently charging."
Wscript.Echo "Battery has", objEvent.BatteryRechargeTime, "minutes until it is fully charged."
Case 7
Wscript.Echo "Battery is currrently charging and has high charge."
Wscript.Echo "Battery has", objEvent.BatteryRechargeTime, "minutes until it is fully charged."
Case 8
Wscript.Echo "Battery is currrently charging and has low charge."
Wscript.Echo "Battery has", objEvent.BatteryRechargeTime, "minutes until it is fully charged."
Case 9
Wscript.Echo "Battery is currrently charging and has critically low charge."
Wscript.Echo "Battery has", objEvent.BatteryRechargeTime, "minutes until it is fully charged."
Case 10
Wscript.Echo "Battery doesn't know what's going on."
Case 11
Wscript.Echo "Battery is partially charged."
End Select

End Sub

希望这对您有所帮助,如果您有任何问题,请给我留言。

关于windows - 记录电源/电池事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com