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

VBS脚本if语句执行逻辑错误 点击Yes却运行vbNo分支如何修复

VBS脚本逻辑错误修复方案

错误原因

  • 没有使用MsgBox的返回值变量X做判断:vbYes、vbNo是VBS内置常量,值分别为6、7,二者都是非零值,直接写if vbNo then相当于判断条件永远为真,代码会优先执行第一个永远成立的分支,和用户点击的选项完全无关。
  • 次要问题:MsgBox第三个参数ErR0r未加引号,会被识别为未定义变量,运行时可能触发额外报错。

修复后代码

' 弹窗标题补充引号,返回值存入变量X
X=MsgBox("Please Restart. Your system will break down.",4+4096, "ErR0r")
' 用返回值和常量对比判断用户选择
if X = vbNo then
    X=MsgBox("e")
    dim count
    set object = wscript.CreateObject("wscript.shell")

    do
        object.run "error.vbs"
        count = count + 1
        ' 原循环次数过大易触发数值溢出,测试可改小
    loop until count = 100
end if

if X = vbYes then
    Dim IntCounter
    Dim objWshShl : Set objWshShl = WScript.CreateObject("wscript.shell")
    Dim objVoice : Set objVoice = WScript.CreateObject("sapi.spvoice")

    ShutdownWarning()
    TimedMessageBox()
    ShutdownComputer()

    Function ShutdownWarning
        X=MsgBox("Thou hath been tricked", 0+4096)
        WScript.Sleep 5000
    End Function

    Function TimedMessageBox
        For IntCounter = 5 To 1 Step -1
            objWshShl.Popup "Your time will be wasted in " _
            & IntCounter & " seconds",1,"Computer Shutdown", 0+4096
        Next
    End Function

    Function ShutdownComputer
        objWshShl.Run "Shutdown /s /f /t 0",0
    End Function
end if

其他说明

原代码的循环终止值设置过大,运行时会触发数值溢出错误,实际使用建议把终止值调整到100以内。整蛊脚本仅可在个人所有的设备上测试使用,不得用于破坏他人设备。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 07:15:08