我需要在Visual Basic脚本中执行命令行

use*_*355 9 vbscript command-line cmd exec

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

我试过这个,但是不行:

Function ExecuteWithTerminalOutput(cmd)
Set shell = WScript.CreateObject("WScript.Shell")
Set Exec =  shell.Exec("ver")
End Function
Run Code Online (Sandbox Code Playgroud)

小智 22

有一种方法可以在不必将输出写入文件的情况下执行此操作.

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

使用VBScript中的以下功能,您可以输入:

thisDir = getCommandOutput("cmd /c dir c:")
Run Code Online (Sandbox Code Playgroud)

当执行上面的行时,变量"thisDir"将包含DIR命令的输出.

请注意,你想从输出某些命令将要求您通过命令外壳(以下简称"CMD/C"以上的部分),通过他们,而如果你不直接shell中运行他们其他人可能会正常工作.没有命令shell就试试吧.如果失败,请使用命令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
Run Code Online (Sandbox Code Playgroud)


Dav*_*amp 13

尝试这样的事情:

Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c ver"
Set objShell = Nothing
Run Code Online (Sandbox Code Playgroud)

编辑:

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

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
Run Code Online (Sandbox Code Playgroud)