Mombu the Microsoft Forum sponsored links

Go Back   Mombu the Microsoft Forum > Microsoft > Windows 2003 Server (TECHNET) > Shell.Exec, Won't Capture STDOUT and STDERR
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 12th April 19:01
maggotchild
External User
 
Posts: 1
Default Shell.Exec, Won't Capture STDOUT and STDERR


Heres the VB code:

Option ********

Dim Shell, X
Set Shell = CreateObject("WScript.Shell")
X = Execute()
WScript.Echo "Status " & X
Set Shell = Nothing

Function Execute()

Dim Retval, P

Set P = Shell.Exec("bs.bat")

While P.Status = 0
If Not P.StdOut.AtEndOfStream Then WScript.Echo "O " &
P.StdOut.ReadAll
If Not P.StdErr.AtEndOfStream Then WScript.Echo "E " &
P.StdErr.ReadAll
WScript.Sleep 100
Wend

Execute = P.ExitCode

End Function

and the batch file:

echo "Assssssssssssssss!"
echo "Bassssssssssssssss!"
echo "Massssssssssssssss!"


Output:

$ cscript.exe vdub2.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

O
c:\Do***ents and Settings\assbasscass\Desktop>echo
"Assssssssssssssss!"
"Assssssssssssssss!"

c:\Do***ents and Settings\assbasscass\Desktop>echo
"Bassssssssssssssss!"
"Bassssssssssssssss!"

c:\Do***ents and Settings\assbasscass\Desktop>echo
"Massssssssssssssss!"
"Massssssssssssssss!"

Status 0


The vbs script detects STDOUT has data, as it prints out the "O", yet
the output from the script isnt there. Any Ideas?

Thanks
  Reply With Quote


  sponsored links


2 12th April 19:01
tom lavedas
External User
 
Posts: 1
Default Shell.Exec, Won't Capture STDOUT and STDERR


The problem is that the WScript.Echo out put is sent to the console's
StdOut, so you never see it, because you script has taken control. I
believe the only way around this is to send the output to the CON
alias for this interface. For example, ...

Option ********

Dim X

X = Doit()
WScript.Echo "Status " & X

Function Doit()

Dim P, Shell, oCon

Set Shell = CreateObject("WScript.Shell")
Set P = Shell.Exec("bs.bat")
' Open a connection to the console device
Set oCon = CreateObject("Scripting.FileSystemObject") _
.OpenTextFile("CON", 2)

While P.Status = 0
If Not P.StdOut.AtEndOfStream Then
oCon.Writeline "StdOut " & P.StdOut.Readall
end if
If Not P.StdErr.AtEndOfStream Then
oCon.Writeline "StdErr " & P.StdErr.Readall
end if
WScript.Sleep 20
Wend

Doit = P.ExitCode

End Function

Tom Lavedas
==========
http://members.cox.net/tglbatch/wsh/
  Reply With Quote
3 13th April 16:28
tom lavedas
External User
 
Posts: 1
Default Shell.Exec, Won't Capture STDOUT and STDERR


{snipped}

I realized there is another approach (see comments below).


' This also works and is probably a better approach
' Use a connection to StdOut outside of the one
' that the Exec method uses
Set oCon = wsh.stdout


Tom Lavedas
==========
http://members.cox.net/tglbatch/wsh/
  Reply With Quote
4 14th April 02:32
maggotchild
External User
 
Posts: 1
Default Shell.Exec, Won't Capture STDOUT and STDERR


Function Doit()


Trying to get this straight. As it seems that this shouldn't cause a
problem (although it does).

So in Windows, Exec spawns a new process whose standard streams are
not inherited from the parent (I say this because I actually have to
read in the data output by the child via it's StdOut/Err properties).

If thats the case, how will outputting to the scripts stdout, step on
the child processes stdout?

Thanks Tom
  Reply With Quote
5 14th April 02:32
maggotchild
External User
 
Posts: 1
Default Shell.Exec, Won't Capture STDOUT and STDERR


Wait, I am in idiot.
My last statements are true... so this does indeed work:
<snip>
If Not P.StdOut.AtEndOfStream Then WScript.Echo "O " & P.StdOut.ReadAll
<snip>
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666