;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       wshPipe() 
;; 
;;ACTION         Runs a specified command and returns STDOUT and STDERR data 
;; 
;;AUTHOR         Glenn Barnas 
;;		 Based on the original WshPipe() by Christopher Shilt 
;; 
;;VERSION        2.3  - 2017/04/23 
;; 
;;HISTORY        2.0  - 2009/10/28 - Production Release 
;;               2.1  - 2012/02/01 
;;                add small delay to return command exit status in 
;;                all cases; return the error number and string of 
;;                failed CreateObject call in elements 1 & 2 of the 
;;                STDERR part of the outer array. 
;;                Based on recommendations by "Witto" 
;;               2.2  - 2016/03/09 
;;                implemented a counter to the "small delay" to limit 
;;                how long the function waits for the completion status. 
;;                Certain commands/situations caused an infinite loop. 
;;                Added an optional parameter to define the delay. 
;;                Seemed to start occurring with Windows 10. 
;;                The default delay is 2s. 
;;               2.3  - 2017/04/23 
;;                Added an optional parameter to return only STDOUT or STDERR 
;; 
;;SYNTAX         wshPipe(Command [, MaxDelay] [, Part]) 
;; 
;;PARAMETERS     Command - REQUIRED - command string to execute 
;; 
;;               MaxDelay - the maximum number of milliseconds to wait for status 
;;               after a command completes. The default is 2000ms (2 seconds). 
;;               This should rarely need to be changed, but usually will be a lower 
;;               value to reduce wait time. (This is a "safety valve" to prevent 
;;               an infinite loop condition when an app doesn't provide status.) 
;; 
;;               Part - OPTIONAL - A numeric value indicating the part of the  
;;               data to be returned. May be useful for apps that only return 
;;               STDERR. 
;;                 0 / undefined - return STDOUT and STDERR 
;;                 1 - return STDOUT only 
;;                 2 - return STDERR only 
;; 
;;REMARKS        This version returns separate arrays for STDOUT and STDERR, 
;;               allowing easy distinction of error messages. This version 
;;               runs silently, conforming to new standards of UDFs not 
;;               generating output. 
;; 
;;RETURNS        Array of arrays - StdOut and STDERR 
;;               Outer array has 2 elements - STDOUT and STDERR, each with an 
;;               inner array Inner arrays contain one element per line of text 
;;;              returned by the command's output stream. 
;;               If WSH is unsupported, the inner array contains 3 elements: 
;;                0 = "WScript.Shell Exec Unsupported" in STDOUT and STDERR 
;;                1 = @ERROR from CreateObject call in STDERR only 
;;                2 = @SERROR from CreateObject call in STDERR only 
;; 
;;DEPENDENCIES   WSH 5.6 (installed with IE 6.0) 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES        
;;                ; Display all files in C:\ directory, display STDOUT 
;;               'All files:' ? 
;;               $SOE = WshPipe('%COMSPEC% /c dir c:\') 
;;               Join($SOE[0], @CRLF) ? 
;;               @ERROR ' | ' @SERROR ? ? 
;; 
;;               ; Display all files in C:\ directory, no display 
;;               'Results only:' ? 
;;               $SOE = WshPipe('%COMSPEC% /c dir c:\') 
;;               @ERROR ' | ' @SERROR ? ? 
;; 
;;               ; Display all files in C:\ directory, no display, Enumerate results 
;;               'Directories only:' ? 
;;               $SOE = WshPipe('%COMSPEC% /c dir c:\') 
;;               for each $line in $SOE[0] 
;;                  If InStr($Line, '<DIR>')  
;;                     $Line ? 
;;                  EndIf  
;;               Next  
;;               @ERROR ' | ' @SERROR ? ? 
; 
Function WshPipe($_ShellCMD, OPTIONAL $_Wait, OPTIONAL $_Part)
 
  Dim $_aTmp[2]						; temp var 
  Dim $_oExec						; Exec object pointer 
  Dim $_Output[1]					; output array 
  Dim $_Exit						; exit status 
  Dim $_C						; Loop counter 
 
  $_Part = Val(0 + $_Part)
 
  If $_Wait = Val($_Wait)
    $_Wait = Val($_Wait) / 10				; convert to hundredths of a second 
  Else
    $_Wait = 200					; default to 2 seconds 
  EndIf
 
  ; run the command 
  $_oExec = CreateObject("WScript.Shell").Exec($_ShellCMD)
  ; Check for correct vartype returned 
  If Not VarType($_oExec) = 9
    $_aTmp[0] = 'WScript.Shell Exec Unsupported'	; set error message 
    $_Output[0] = $_aTmp				; store in STDOUT array 
    $_aTmp[1] = @ERROR					; return error code from CreateObject 
    $_aTmp[2] = @SERROR					; return error string from CreateObject 
    $_Output[1] = $_aTmp				; store in STDERR array 
    $_Exit = 10
  Else
    $_C = 0
    ; Wait for completion and gather the STDOUT and STDERR text into arrays of arrays 
    ; the delay time is set to 10s or the number of ms defined by the _Wait parameter. 
    While Not $_oExec.Status And $_C < $_Wait
      Sleep 0.01
      $_C = $_C + 1
    Loop
    If $_Part = 0 Or $_Part = 1
      $_Output[0] = Split($_oExec.StdOut.ReadAll, @CRLF)
    EndIf
    If $_Part = 0 Or $_Part = 2
      $_Output[1] = Split($_oExec.StdErr.ReadAll, @CRLF)
    EndIf
    $_Exit = $_oExec.ExitCode
  EndIf
 
 
  ; Return the data and exit status 
  $WshPipe = $_Output
  Exit $_Exit
 
EndFunction