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

时间:2011-11-01 13:32:21

标签: vbscript wmi hta wmic

我有一个使用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

1 个答案:

答案 0 :(得分:3)

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