Witto
(MM club member)
2012-01-30 02:24 PM
Shell Function that returns StdOut and Exit or Error value

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...


AllenAdministrator
(KiX Supporter)
2012-01-30 02:39 PM
Re: Shell Function that returns StdOut and Exit or Error value

Fortunately, Glenn did something like this. Unfortunately, its not here to find, its on his site....

http://www.innotechcg.com/tech/main.asp?ID=1:KixLib:WshPipe.htm


Glenn BarnasAdministrator
(KiX Supporter)
2012-01-30 02:43 PM
Re: Shell Function that returns StdOut and Exit or Error value

I didn't publish it because it was similar to the other WshPipe function. I'll add it here if you think it's useful.

The other function combined STDOUT and STDERR into a single return string. I needed separate STDOUT and STDERR values as well as the exit status.

Glenn


AllenAdministrator
(KiX Supporter)
2012-01-30 02:50 PM
Re: Shell Function that returns StdOut and Exit or Error value

I just remembered looking through your site a while back and seeing updated versions of UDFs... for whatever reason I remembered this one specifically just now. It is a good idea.

Glenn BarnasAdministrator
(KiX Supporter)
2012-01-30 02:53 PM
Re: Shell Function that returns StdOut and Exit or Error value

OK - I'll post it later today.

Glenn


Witto
(MM club member)
2012-01-30 03:28 PM
Re: Shell Function that returns StdOut and Exit or Error value

It feels like I just invented the wheel.
Returning an array of arrays should have been my next invention.


AllenAdministrator
(KiX Supporter)
2012-01-30 04:03 PM
Re: Shell Function that returns StdOut and Exit or Error value

LOL! Been there, done that too.

Glenn BarnasAdministrator
(KiX Supporter)
2012-01-30 04:17 PM
Re: Shell Function that returns StdOut and Exit or Error value

FYI - there are 140 UDF files on our site, including several libraries of functions to interface with Excel, SQL/Access DB, WSUS, and the task scheduler. Our entire development library is PostPrepped every night onto our web server, so the latest version of UDFs can be found there.

Here is the link to our Kixtart UDF Library.

Glenn