Command line 我需要在Visual Basic脚本中执行命令行
我需要在我的vbs中执行命令“ver”来查看我的操作系统版本,我不知道如何制作它 我试过这个,但不起作用:Command line 我需要在Visual Basic脚本中执行命令行,command-line,vbscript,cmd,exec,Command Line,Vbscript,Cmd,Exec,我需要在我的vbs中执行命令“ver”来查看我的操作系统版本,我不知道如何制作它 我试过这个,但不起作用: Function ExecuteWithTerminalOutput(cmd) Set shell = WScript.CreateObject("WScript.Shell") Set Exec = shell.Exec("ver") End Function 试着这样做: Dim objShell Set objShell = WScript.CreateObject ("WScr
Function ExecuteWithTerminalOutput(cmd)
Set shell = WScript.CreateObject("WScript.Shell")
Set Exec = shell.Exec("ver")
End Function
试着这样做:
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
有一种方法可以做到这一点,而不必将输出写入文件 例如,假设您想要捕获目录列表的文本。(有很多比这更好的方法,但我只是用一个简单的例子。) 使用VBScript中的以下函数,可以输入:
thisDir = getCommandOutput("cmd /c dir c:")
当执行上述行时,变量“thisDir”将包含DIR命令的输出
请注意,您希望从中输出的某些命令将要求您通过命令shell(上面的“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
好的,很好,它可以工作,但是我需要一个var中的结果,用于If,我可以这样做吗?谢谢你的帮助!谢谢兄弟!成功了!有一件事,“;”不需要它。很好的帮助,太快了!
Dim shell
Set shell= WScript.CreateObject ("WScript.shell")
shell.Exec"cmd /c ver"
Set shell= Nothing