#204138 - 2012-01-30 02:24 PM
Shell Function that returns StdOut and Exit or Error value
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
I was searching how to get the Exit or Error value from a command launched with "Shell".
I found the article TechNet Blogs > Hey, Scripting Guy! Blog > How Can I Hide the Command Window When Executing a Command Like net Localgroup Administrators? that shows this is possible with WScript.Shell.Exec($Command)
I created this function
;;;;;;;;;;;;;;;;;; ; Script Options ; ;;;;;;;;;;;;;;;;;;
If Not @LOGONMODE Break On Else Break Off EndIf Dim $RC $RC = SetOption("Explicit", "On") $RC = SetOption("NoMacrosInStrings", "On") $RC = SetOption("NoVarsInStrings", "On") If @SCRIPTEXE = "KIX32.EXE" $RC = SetOption("WrapAtEOL", "On") EndIf ;;;;;;;;;;;;;;;;;;;;; ; Declare variables ; ;;;;;;;;;;;;;;;;;;;;;
Dim $Stdout Dim $ReturnValue Dim $Command
;;;;;;;;;;;;;;;;;;;;;;;; ; Initialize variables ; ;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; ; Code ; ;;;;;;;;
$Command = ExpandEnvironmentVars('"%comspec%"') + ' /C Dir C:\' $StdOut = WshShellExec($Command) $ReturnValue = @ERROR
? 'Command:' ? $Command ? 'stdOut:' ? $stdOut ? 'Error:' ? $ReturnValue ?
$Command = ExpandEnvironmentVars('"%comspec%"') + ' /C Dir Q:\' $StdOut = WshShellExec($Command) $ReturnValue = @ERROR
? 'Command:' ? $Command ? 'stdOut:' ? $stdOut ? 'Error:' ? $ReturnValue ?
;;;;;;;;;;;;;;; ; UDF Section ; ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;; ; Personal UDF Section ; ;;;;;;;;;;;;;;;;;;;;;;;;
Function WshShellExec($Command) Dim $RC Dim $WshShell $WshShell = CreateObject('WScript.Shell') $RC = $WshShell.Exec($Command) While Not $RC.Status Sleep 1 Loop $WshShellExec = $RC.StdOut.ReadAll Exit $RC.ExitCode EndFunction |
Does anybody know how to include the value $RC.StdErr.ReadAll in the return value $WshShellExec?
I was thinking to return an array
$WshShellExec = Split($RC.StdOut.ReadAll + '|' + $RC.StdErr.ReadAll,'|')
But that does not work...
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(mole)
and 1551 anonymous users online.
|
|
|