【问题标题】:Why does VBScript sometimes block in WshShell.Exec?为什么 VBScript 有时会在 WshShell.Exec 中阻塞?
【发布时间】:2011-11-01 13:32:21
【问题描述】:

我有一个使用 WshShell.Exec 获取 Windows 版本的 html 应用程序 (HTA)。我正在使用wmic os get Caption 获取特定版本,该版本在命令行和批处理脚本中运行良好。我还测试了我调用WshShell.Exec 的方式,它可以与其他命令(即echo Windows 2008)一起正常工作。当我尝试将这些东西结合起来时,就会出现问题,而 Exec 似乎只是冻结了。你能推荐一个解决这个问题的方法吗?这是我的代码:

Function GetWinVersion
    'Returns 2008, XP, or 7
    set WshShell = CreateObject("WScript.Shell")
    set oExec = WshShell.Exec("wmic os get Caption")
    do while oExec.Status = 0
        'I added this very busy wait, though it doesn't seem to help
        'Would sleep if it was available in an hta
    loop
    While oExec.StdOut.AtEndOfStream <> True
        thisLine = oExec.StdOut.ReadLine
        'MsgBox "Found line: " & thisLine
        if InStr(thisLine, "2008") > 0 then
            GetWinVersion=2008
            Exit Function
        elseif InStr(thisLine, "XP") > 0 then
            GetWinVersion=XP
            Exit Function
        elseif InStr(thisLine, "Windows 7") > 0 then
            GetWinVersion=7
            Exit Function
        end if
    Wend
    MsgBox "Error parsing output of wmic os get Caption"
    self.Close
End Function

【问题讨论】:

    标签: vbscript wmi hta wmic


    【解决方案1】:

    WMIC是WMI的封装,可以直接在VBS中使用;

    function GetWinVersion
        dim WMI: set WMI = GetObject("winmgmts:\\.\root\cimv2")
        dim colResults: set colResults = WMI.ExecQuery("Select * from Win32_OperatingSystem")
        dim item
        for each item in colResults
            GetWinVersion = item.caption
        next
    end function
    

    【讨论】:

    • +1 这让我的代码正常工作。谢谢。关于 Exec 为何阻塞的任何想法?我在其他地方也听说过这会导致问题,并想知道为什么我不会再被抓到。
    • 不,我得到了相同的结果,尝试检查 StdErr 的输出?
    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2017-07-14
    相关资源
    最近更新 更多