谁能帮我修复 Windows 10 上的 VBS 脚本,该脚本在电池电量不足时显示 warning/MsgBox?

Can anyone help me with fixing a VBS script on Windows 10 that displays a warning/MsgBox on low battery?

我想做的是编写一个脚本,在我的计算机电池电量降至 20% 时创建一条消息 box/popup。我希望消息框只出现一次 - 在我单击“确定”后它不应该再次出现,除非我为我的计算机充电并让它再次耗尽到 20%。

问题是当我的电池电量低于 20% 时,我的代码会导致每隔几分钟出现一次消息框。我的代码如下:

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

while (1) 
    set oResults = oServices.ExecQuery("select * from batterystatus")
    for each oResult in oResults
        iRemaining = oResult.RemainingCapacity
        bCharging = oResult.Charging
    next
    iPercent = Round((iRemaining / iFull) * 100)
    
    if iRemaining and not bCharging and (iPercent < 21) and (iPercent > 10) Then msgbox "Your battery power is low (" & iPercent & "%). If you need to continue using your computer, either plug in your computer, or shut it down and then change the battery.",vbInformation, "Your battery is running low."
    wscript.sleep 30000 ' 5 minutes
wend

我对 VBScript(和一般编程)还很陌生,所以我不太确定如何解决这个问题。

您需要有一个标志,例如一个名为 iShow 的变量。它以值 1 开始(可以弹出消息),单击确定后,iShow 将为零(不再显示)。下一次此标志的值为 1 是在电池电量超过 21% 时,因此当它再次下降到低于 21% 时,消息可以再次弹出,但只会弹出一次。这是代码:

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

Dim iShow
iShow=1

while (1) 
    set oResults = oServices.ExecQuery("select * from batterystatus")
    for each oResult in oResults
        iRemaining = oResult.RemainingCapacity
        bCharging = oResult.Charging
    next
    iPercent = Round((iRemaining / iFull) * 100)
    
    if (iPercent>21) then iShow=1
    if (iShow=1) and not bCharging and (iPercent < 21) Then
         msgbox "Your battery power is low (" & iPercent & "%). If you need to continue using your computer, either plug in your computer, or shut it down and then change the battery.",vbInformation, "Your battery is running low."
         iShow=0
    end if
    wscript.sleep 30000 ' 5 minutes
wend