Page 1 of 1 1
Topic Options
#204138 - 2012-01-30 02:24 PM Shell Function that returns StdOut and Exit or Error value
Witto Offline
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
#204139 - 2012-01-30 02:39 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Witto]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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

Top
#204140 - 2012-01-30 02:43 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204141 - 2012-01-30 02:50 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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.
Top
#204142 - 2012-01-30 02:53 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
OK - I'll post it later today.

Glenn


Edited by Glenn Barnas (2012-01-30 03:03 PM)
Edit Reason: or now!
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204144 - 2012-01-30 03:28 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Glenn Barnas]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
It feels like I just invented the wheel.
Returning an array of arrays should have been my next invention.

Top
#204146 - 2012-01-30 04:03 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Witto]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
LOL! Been there, done that too.
Top
#204147 - 2012-01-30 04:17 PM Re: Shell Function that returns StdOut and Exit or Error value [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 507 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.058 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org