11

我需要在我的 vbs 中执行命令“ver”来查看我的操作系统的版本,我不知道如何制作它。

我试过这个,但不工作:

Function ExecuteWithTerminalOutput(cmd)
Set shell = WScript.CreateObject("WScript.Shell")
Set Exec =  shell.Exec("ver")
End Function
4

3 回答 3

23

有一种方法可以做到这一点,而不必将输出写入文件。

例如,假设您想要捕获目录列表的文本。(有很多比这更好的方法,但我只是使用一个简单的例子。)

使用 VBScript 中的以下函数,您可以输入:

thisDir = getCommandOutput("cmd /c dir c:")

当执行上述行时,变量“thisDir”将包含 DIR 命令的输出。

请注意,您想要输出的某些命令将要求您通过命令外壳(上面的“cmd /c”部分)传递它们,而如果您直接在没有外壳的情况下运行它们,其他命令可能会正常工作。在没有命令外壳的情况下尝试它。如果失败,请使用命令 shell 尝试。

'
' Capture the results of a command line execution and
' return them to the caller.
'
Function getCommandOutput(theCommand)

    Dim objShell, objCmdExec
    Set objShell = CreateObject("WScript.Shell")
    Set objCmdExec = objshell.exec(thecommand)
    getCommandOutput = objCmdExec.StdOut.ReadAll

end Function
于 2012-08-09T16:14:04.420 回答
13

尝试这样的事情:

Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c ver"
Set objShell = Nothing

编辑:

那么你可以将输出重定向到一个文件,然后读取该文件:

return = WshShell.Run("cmd /c ver > c:\temp\output.txt", 0, true)

Set fso  = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("c:\temp\output.txt", 1)
text = file.ReadAll
file.Close
于 2012-07-16T09:02:59.140 回答
1
Dim shell
Set shell= WScript.CreateObject ("WScript.shell")
shell.Exec"cmd /c ver"
Set shell= Nothing
于 2019-04-16T09:25:51.033 回答