可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This is what I have so far. It works; outputing the folder path to temp to a text file. What I really want, is to output the data to a variable. Every example I see online, show how to do this using something like:
set objScriptExec = wshShell.Exec (strCommand)
followed by
strresult = LCase(objScriptExec.StdOut.ReadAll. // code
I want this to run with Run
, not Exec
, because I want the command prompt windows to be hidden as I will performing many commands with the code below. How can I capture that output to a variable?
Set wsShell = CreateObject("WScript.Shell") strCommand = "cmd /c echo %temp% > %temp%\test.txt" wsShell.Run strcommand,0,True
回答1:
This may be done with the Windows Script Host Exec command. StdOut, StdIn, and StdErr may all be accessed, and ERRORLEVEL is available when the command completes.
Dim strMessage, strScript, strStdErr, strStdOut Dim oExec, oWshShell, intErrorLevel Dim ComSpec Set oWshShell = CreateObject("WScript.Shell") ComSpec = oWshShell.ExpandEnvironmentStrings("%comspec%") intErrorLevel = 0 strScript = ComSpec & " /C echo %temp%" On Error Resume Next Set oExec = oWshShell.Exec (strScript) If (Err.Number <> 0) Then strMessage = "Error: " & Err.Message intErrorLevel = 1 Else Do While oExec.Status = 0 Do While Not oExec.StdOut.AtEndOfStream strStdOut = strStdOut & oExec.StdOut.ReadLine & vbCrLf Loop Do While Not oExec.StdErr.AtEndOfStream strStdErr = strStdErr & oExec.StdErr.ReadLine & vbCrLf Loop WScript.Sleep 0 Loop intErrorLevel = oExec.ExitCode strMessage = strStdOut & strStdErr & CStr(intErrorLevel) End If WScript.Echo (strMessage)
NOTE: Replacing "ReadLine" above with "Read(1)" accomplishes the same thing, but adds an ability to process characters rather than whole lines.
回答2:
Of course Wscript.Shell
would be a lot easier, but, since you want more fine grain control of your session, consider using Win32_Process
. Usually, one uses this to control the placement of a new window, but, in your case, you want it hidden, so I set startupInfo.ShowWindow = 0
which means SW_HIDE
. The following declares a VBScript
function called RunCmd
and which will run a command in an invisible window saving the output to a text file and then return the contents of the text file to the caller. As an example, I invoke RunCmd
with the HOSTNAME
command:
Function RunCmd(strCmd) Dim wmiService Set wmiService = GetObject("winmgmts:\\.\root\cimv2") Dim startupInfo Set startupInfo = wmiService.Get("Win32_ProcessStartup") Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim cwd cwd = fso.GetAbsolutePathname(".") startupInfo.SpawnInstance_ startupInfo.ShowWindow = 0 ' startupInfo.X = 50 ' startupInfo.y = 50 ' startupInfo.XSize = 150 ' startupInfo.YSize = 50 ' startupInfo.Title = "Hello" ' startupInfo.XCountChars = 36 ' startupInfo.YCountChars = 1 Dim objNewProcess Set objNewProcess = wmiService.Get("Win32_Process") Dim intPID Dim errRtn errRtn = objNewProcess.Create("cmd.exe /c """ & strCmd & """ > out.txt", cwd, startupInfo, intPID) Dim f Set f = fso.OpenTextFile("out.txt", 1) RunCmd = f.ReadAll f.Close End Function MsgBox RunCmd("HOSTNAME")
References: