You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Windows10下实现低电量单次提醒的VBS脚本重复弹窗问题

VBS低电量提醒脚本修正方案

核心改动逻辑

  • 新增hasAlerted标记变量,记录当前放电周期是否已经弹出过低电量提醒
  • 触发提醒时额外判断标记状态,保证单次放电周期仅触发一次提醒
  • 检测到设备处于充电状态时自动重置标记,下次放电到阈值可重新触发提醒

修正后完整代码

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 hasAlerted : hasAlerted = False

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 bCharging Then
        hasAlerted = False
    End If
    
    ' 新增hasAlerted判断,保证仅弹一次
    if iRemaining and not bCharging and (iPercent < 21) and (iPercent > 10) and (hasAlerted = False) 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."
        ' 弹出后标记为已提醒
        hasAlerted = True
    End If
    wscript.sleep 300000 ' 原注释标注为5分钟,原参数30000对应30秒,此处已修正为5分钟对应参数
wend

补充说明

如果需要调整检测频率,修改wscript.sleep的参数即可,参数单位为毫秒,1分钟对应60000。

内容的提问来源于stack exchange,提问作者Zodiiak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 10:57:04