这是一个VBScript代码示例,展示了如何捕获命令行程序发送到标准输出的内容。它执行命令xcopy /?并在消息框中显示输出。在消息框出现之前,您会在瞬间看到弹出的控制台窗口。
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec("xcopy /?")
Do
line = objExec.StdOut.ReadLine()
s = s & line & vbcrlf
Loop While Not objExec.Stdout.atEndOfStream
WScript.Echo s下面是另一个VBScript代码示例,它展示了如何在不显示控制台窗口的情况下执行脚本。
objShell.Run "c:\temp\mybatch.bat C:\WINDOWS\system32\cmd.exe", 0或
objShell.Run "c:\temp\myscript.vbs C:\WINDOWS\system32\cscript.exe", 0如您所见,它的形式为<script><space><executor>。最后一个示例使用objShell.Run而不是objShell.Exec
我不知道的是如何执行命令行程序(如果需要的话,从批处理文件),捕获标准输出,而不显示控制台窗口。有什么想法吗?
发布于 2011-02-11 05:55:35
此概念验证脚本:
' pocBTicks.vbs - poor man's version of backticks (POC)
Option Explicit
' Globals
Const SW_SHOWMINNOACTIVE = 7
Const ForReading = 1
Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )
Dim goWSH : Set goWSH = CreateObject( "WScript.Shell" )
' Dispatch
WScript.Quit demoBTicks()
' demoBTicks -
Function demoBTicks()
demoBTicks = 1
Dim aCmds : aCmds = Array( _
"dir pocBTicks.vbs" _
, "dur pocBTicks.vbs" _
, "xcopy /?" _
)
Dim sCmd
For Each sCmd In aCmds
WScript.Echo "########", sCmd
Dim aRet : aRet = BTicks( sCmd )
Dim nIdx
For nIdx = 0 To UBound( aRet )
WScript.Echo "--------", nIdx
WScript.Echo aRet( nIdx )
Next
Next
demoBTicks = 0
End Function ' demoBTicks
' BTicks - execute sCmd via WSH.Run
' aRet( 0 ) : goWSH.Run() result
' aRet( 1 ) : StdErr / error message
' aRet( 2 ) : StdOut
' aRet( 3 ) : command to run
Function BTicks( sCmd )
Dim aRet : aRet = Array( -1, "", "", "" )
Dim sFSpec2 : sFSpec2 = goFS.GetAbsolutePathName( "." )
Dim sFSpec1 : sFSpec1 = goFS.BuildPath( sFSpec2, goFS.GetTempName() )
sFSpec2 = goFS.BuildPath( sFSpec2, goFS.GetTempName() )
aRet( 3 ) = """%COMSPEC%"" /c """ + sCmd + " 1>""" + sFSpec1 + """ 2>""" + sFSpec2 + """"""
Dim aErr
On Error Resume Next
aRet( 0 ) = goWSH.Run( aRet( 3 ), SW_SHOWMINNOACTIVE, True )
aErr = Array( Err.Number, Err.Description, Err.Source )
On Error GoTo 0
If 0 <> aErr( 0 ) Then
aRet( 0 ) = aErr( 0 )
aRet( 1 ) = Join( Array( aErr( 1 ), aErr( 2 ), "(BTicks)" ), vbCrLf )
BTicks = aRet
Exit Function
End If
Dim nIdx : nIdx = 1
Dim sFSpec
For Each sFSpec In Array( sFSpec2, sFSpec1 )
If goFS.FileExists( sFSpec ) Then
Dim oFile : Set oFile = goFS.GetFile( sFSpec )
If 0 < oFile.Size Then
aRet( nIdx ) = oFile.OpenAsTextStream( ForReading ).ReadAll()
goFS.DeleteFile sFSpec
End If
End If
nIdx = nIdx + 1
Next
BTicks = aRet
End Function演示如何使用.Run和临时文件通过隐藏的控制台获取诸如反引号之类的内容。像样的文件处理、在sCmd中引用、清理返回的字符串以及处理编码都需要更多的工作。但是也许你可以使用这个策略来实现一些符合你需要的东西。
发布于 2012-07-11 04:40:28
我通常这样用:
Wscript.echo execStdOut("ping google.com")
Function execStdOut(cmd)
Dim goWSH : Set goWSH = CreateObject( "WScript.Shell" )
Dim aRet: Set aRet = goWSH.exec(cmd)
execStdOut = aRet.StdOut.ReadAll()
End Function 对于更高级的命令,您可以使用wrap to comspec (cmd)
my res = execStdOut("%comspec%" & " /c " & """" & "dir /b c:\windows\*.exe" & """" & " && Echo. && Echo finished") 发布于 2011-07-28 22:56:24
为了将输出重定向到控制台,使用cscript运行脚本,例如:c:\cscript myscript.vbs。
cscript有一些命令行选项。(对我来说)最重要的是开关//NOLOGO。如果你使用它(cscript //nologo myscript.vbs),它将省略微软的商品...
https://stackoverflow.com/questions/4919573
复制相似问题