为什么VBScript有时阻塞在WshShell.Exec



我有一个使用WshShell的html应用程序(HTA)。执行命令获取Windows版本。我使用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

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

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

相关内容

  • 没有找到相关文章

最新更新