【发布时间】:2016-08-26 18:48:31
【问题描述】:
编写 VB 脚本来监控电池百分比。如果大于 95,则拔掉插头。如果 else 部分并休眠。它没有进入“系统不会休眠”部分。
hibernate.bat 是一个简单的使 Windows7 休眠的批处理脚本。
代码是一个永无止境的for 循环,其中 do while 循环用于在 shell 脚本中复制“继续”。当电池电量达到 20% 时,插件充电器有 10 秒的时间。如果收费,则必须进行下一个 for 循环迭代。没有进入“如果收费”循环。为什么?
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oServices = oLocator.ConnectServer(".","root\wmi")
Set oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")
For Each oResult In oResults
iFull = oResult.FullChargedCapacity
Next
For i=1 To 10
Do
i=+2
Set oResults = oServices.ExecQuery("select * from batterystatus")
For Each oResult In oResults
iRemaining = oResult.RemainingCapacity
Charging = oResult.Charging
Discharging = oResult.Discharging
Next
iPercent = ((iRemaining / iFull) * 100) Mod 100
If Charging And (iPercent > 95) Then
MsgBox "Unplug Charger. Battery is at " & iPercent & "%", vbInformation, "Battery monitor"
ElseIf Discharging And (iPercent < 20) Then
MsgBox "Plug-in Charger. Battery is at " & iPercent & "%", vbInformation, "Battery monitor"
WScript.Sleep 10000 ' 10 sec
If Charging Then
CreateObject("WScript.Shell").Popup "System won't hibernate", 5, "Good News!!!"
Exit Do
Else
Set shell = CreateObject("WScript.Shell")
shell.CurrentDirectory = "C:\Users\abcd\Desktop"
CreateObject("WScript.Shell").Popup "System will hibernate", 5, "Hmm..."
shell.Run "hibernate.bat"
Exit For
End If
End If
Loop While False
Next
【问题讨论】:
-
喇叭以重复关闭。
标签: vbscript