2013-08-29 10 views
2

刚刚购买了APC电池备份,并将USB数据线连接到我的Windows 7计算机。它自动安装一个驱动程序,现在在Windows Tray中,我看到一个带有电源插头图标的电池,并显示电量百分比。当我从墙上拔下UPS时,桌面会进入电池模式,小图标会发生变化......就像是笔记本电脑一样。记录功率/电池事件

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

感谢,AD

回答

1

的一种方式做,这是与运行使用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 

希望这会有所帮助,请给我一条线,如果您有任何问题。

+0

无法弄清楚如何放弃你的一条线。我的问题:这个脚本是用什么语言编写的? –

+1

@ 505 - VBScript –