在 VBSCript 中输出 msgbox 时出现问题

Posted

技术标签:

【中文标题】在 VBSCript 中输出 msgbox 时出现问题【英文标题】:Having issues outputting msgbox in VBSCript 【发布时间】:2014-06-10 10:28:28 【问题描述】:

我正在运行一个脚本,该脚本在运行程序之前在 Windows 机器上执行多项检查。我正在运行一个检查补丁状态、防火墙状态和防病毒状态等内容的 VBScript。脚本运行后,需要输出信息,以便最终用户可以修复出现的任何问题。这需要在单个消息框中输出,显示最终用户计算机上不符合要求的所有内容。

现在脚本运行后,我得到了多个输出框,但没有一个显示需要修复的名称。我只是在对话框中的引号中获取信息。

errors = Array(strSUpdate,strFirewallStatus,strProdState)
Dim errorfix

For Each item In errors
    set errorfix = errorfix & item & vbnewline
    if (errors = "Off") Then
        msgbox "Compliance Check results" & vbNewLine & vbNewLine & _
        "The following requires attention" & vbNewLine & errorfix & vbNewLine & _
        "Press OK to Continue", 0, "Moka 5 Compliance Check"
    Else
        msgbox "Compliance Check results" & vbNewLine & vbNewLine & _
        "Everything looks good." & vbNewLine & vbNewLine & _
        "Press OK to Continue", 0, "Moka 5 Compliance Check"
    End if
Next

非常感谢任何帮助

【问题讨论】:

我不认为你想要消息框inside循环,可以吗? @tomalak 你说得对,我没有。我认为我正在寻找的是一种创建变量的方法是数组中存在的条件。因此,如果“strSUpdate”和“strFirewallStatus”为“Off”,那么它将创建两个新变量并输出它们。所以我会查看'For Each item In errors' 和 'if (errors = "Off") Then' 行,它会为每个符合该条件的变量创建一个变量? 【参考方案1】:

我的建议是完全避免使用数组,而改用更方便的dictionaries。

如果不出意外,您将通过这种方式创建更具可读性的代码,但它们也比数组强大得多。

Option Explicit

Dim errors
Set errors = CreateObject("Scripting.Dictionary")

' fill the dictionary troughout your program. Fill in just the 
' actionables and don't insert the stuff that's okay.
errors("Firewall Status") = "Off"
errors("This other thing") = "Missing"

If errors.Count = 0 Then
    MsgBox "Compliance Check results" & vbNewLine & vbNewLine & _
           "Everything looks good." & vbNewLine & vbNewLine & _
           "Press OK to Continue", vbOkOnly, "Moka 5 Compliance Check"
Else
    Dim item, toFix

    For Each item In errors
        toFix = toFix & " - " & item & " is " & errors(item) & vbNewLine
    Next

    Msgbox "Compliance Check results" & vbNewLine & vbNewLine & _
           "The following requires attention:" & vbNewLine & _
           toFix & vbNewLine & _
           "Press OK to Continue", 0, "Moka 5 Compliance Check"
End If

【讨论】:

在我的脚本中,我为变量设置了 IF 语句; strSUpdate、strFirewallStatus 和 strProdState。是否可以将这些变量添加到字典中,如果它们设置为“关闭”,则将它们拉出来? 如果你这样问:当然。 :) 只是有点......创建一个字典条目然后如果有一定的价值就忽略它是徒劳的。请参阅我的代码中的注释。重构脚本永远不会太晚。 很高兴听到!请注意,字典存在一个问题:它们没有保证的迭代顺序(与数组相反)。如果您需要保持某种稳定的顺序,那么不幸的是,您又回到了数组。【参考方案2】:

我认为您需要为 msgbox 输入一个返回值,即 tempresponse = Msgbox("") Link is here

errors = Array(strSUpdate,strFirewallStatus,strProdState)
Dim errorfix, tempresponse

For Each item In errors
    set errorfix = errorfix & item & vbnewline
    if (errors = "Off") Then
        tempresponse = msgbox ("Compliance Check results" & vbNewLine & vbNewLine & _
        "The following requires attention" & vbNewLine & errorfix & vbNewLine & _
        "Press OK to Continue", 0, "Moka 5 Compliance Check")
    Else
        tempresponse = msgbox ("Compliance Check results" & vbNewLine & vbNewLine & _
        "Everything looks good." & vbNewLine & vbNewLine & _
        "Press OK to Continue", 0, "Moka 5 Compliance Check")
    End if
Next

正如@Tomalak 所说,每次迭代后您都需要清除 msgbox,popupwork better, as it will clear the message after x seconds.

【讨论】:

以上是关于在 VBSCript 中输出 msgbox 时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

VBScript MsgBox 总是报告错误。我需要在计算机上安装啥库?

我需要知道用户对 MsgBox 做了啥。非常基本的 VBScript

关闭启动画面时出现类型不匹配错误

VBScript 从 Shell 获取结果

在连接本地WMI提供程序时出现错误 70。没有权限(Microsoft VBScript运行时错误

VBScript基础篇