这是一个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
我不知道的是如何执行命令行程序(如果需要,从批处理文件中),捕获标准输出,而不显示控制台窗口。有什么想法吗?
答案 0 :(得分:14)
我通常使用它:
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
对于更高级的命令,你可以使用comspec(cmd)
my res = execStdOut("%comspec%" & " /c " & """" & "dir /b c:\windows\*.exe" & """" & " && Echo. && Echo finished")
答案 1 :(得分:6)
为了将输出重定向到控制台,请使用cscript运行脚本,例如:c:\cscript myscript.vbs
。
cscript有一些命令行选项。最重要的(对我来说)是开关// NOLOGO。如果yoy使用它(cscript //nologo myscript.vbs
),它将省略Microsoft商品......
答案 2 :(得分:3)
这个概念验证脚本:
' 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中引用,清理返回的字符串以及处理编码都需要更多的工作。但也许您可以使用该策略来实现符合您需求的东西。
答案 3 :(得分:1)
如果您不介意出现任务栏按钮,则可以在启动控制台窗口之前将其移出屏幕。
如果存在HKCU\Console\WindowPosition
密钥,Windows将使用其值来定位控制台窗口。如果密钥不存在,您将获得一个系统定位窗口。
因此,保存此密钥的原始值,设置您自己的值以将其置于屏幕外,调用Exec()
并捕获其输出,然后恢复密钥的原始值。
WindowPosition
密钥需要32位值。高字是X坐标,低字是Y坐标(XXXXYYYY
)。
With CreateObject("WScript.Shell")
' Save the original window position. If system-positioned, this key will not exist.
On Error Resume Next
intWindowPos = .RegRead("HKCU\Console\WindowPosition")
On Error GoTo 0
' Set Y coordinate to something crazy...
.RegWrite "HKCU\Console\WindowPosition", &H1000, "REG_DWORD"
' Run Exec() and capture output (already demonstrated by others)...
.Exec(...)
' Restore window position, if previously set. Otherwise, remove key...
If Len(intWindowPos) > 0 Then
.RegWrite "HKCU\Console\WindowPosition", intWindowPos, "REG_DWORD"
Else
.RegDelete "HKCU\Console\WindowPosition"
End If
End With
如果确实想要确保坐标在屏幕外,您可以使用IE或其他工具通过VBScript获取屏幕尺寸。
答案 4 :(得分:0)
在VBA中返回G:\ OF
中的所有子文件夹sub M_snb()
c00= createobejct("wscript.Shell").exec("cmd /c Dir G:\OF\*. /s/b").stdout.readall
end sub
将返回的字符串拆分为数组
sub M_snb()
sn=split(createobejct("wscript.Shell").exec("cmd /c Dir G:\OF\*. /s/b").stdout.readall,vbCrLf)
for j=0 to ubound(sn)
msgbox sn(j)
next
End Sub
答案 5 :(得分:0)
这是获取命令行StdOut(结果)而无需在vbscript中看到此弹出式黑色dos窗口的方法:
Set Sh = CreateObject("WScript.Shell")
tFile=Sh.ExpandEnvironmentStrings("%Temp%")&"\t.txt"
Sh.Run "cmd.exe /c xcopy /? > """&tFile&""" ",0,False
Wscript.echo CreateObject("Scripting.FileSystemObject").openTextFile(tFile).readAll()
答案 6 :(得分:-1)
而不是WScript.Shell
,请考虑使用Win32_Process
与startupInfo.ShowWindow = 0
一起使用SW_HIDE
启动流程。我在VBS Run cmd.exe output to a variable; not text file下发布了一个详细的例子。