Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Capturing command-line output

16 views
Skip to first unread message

Jeff Wright

unread,
Jul 2, 2002, 2:17:52 PM7/2/02
to
I just did a google search for info on this and got no clear answer.

What is the best way to capture output from a command prompt command.

I am trying to capture the output from hfnetchk.exe. A text file would
be fine. But I would like to remove a chuck of the output for my final
output file.

Running "hfnetchk.exe -s 2 -o tab" on a machine named FRODO produces
the following output..of which I only want the section that starts
"Scanning FRODO until the Done scanning FRODO:


-----------------------------------------------------------------------------------------------------------------
C:\tools>hfnetchk -s 2 -o tab
Microsoft Network Security Hotfix Checker, 3.32
Copyright (C) Shavlik Technologies, 2001-2002
Developed for Microsoft by Shavlik Technologies, LLC
in...@shavlik.com (www.shavlik.com)


Please use the -v switch to view details for
Patch NOT Found, Warning and Note messages


Attempting to download the CAB from:
http://download.microsoft.com/download/xml/security/1.0/NT5/EN-US/mssecure.cab

File was successfully downloaded.

Attempting to load C:\tools\mssecure.xml.
Using XML data version = 1.0.1.308 Last modified on 6/26/2002.
Machine Name Product Bulletin Q Number Reason Status

Scanning FRODO
.......................FRODO (1**.***.***.***) WINDOWS XP GOLD
All necessary hotfixes have been applied. Information
.......FRODO (1**.***.***.***) INTERNET EXPLORER 6 GOLD
All necessary hotfixes have been applied. Information

Done scanning FRODO

-------------------------------------------------------------------------------------------------------------


Thanks,

Jeff Wright

Jeff Wright

unread,
Jul 2, 2002, 2:36:46 PM7/2/02
to
Oh, BTW, I found the function CMDResults below doing a google search
(thanks to Torgeir)..it works well enough. For some reason the output
that goes to the file is exactly what I want...see that after the code
below.

--------------------------------------------------------------

sCmd="c:\tools\hfnetchk -s 2 -o tab"

' Get the output of "sCmd" into a text string
sResult = CMDResults(sCmd)

' Wscript.Echo sResult
' Msgbox sResult

filename = "C:\patch_report.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename,8,0)
f.write sResult & vbCrLf
f.close

Function CMDResults(cmdline)
' simple wrapper for command line you want
' to use while capturing output
Set oShell = CreateObject("WScript.Shell")
Set oExCmd = oShell.Exec(cmdline)
Set oExCmdStdOut = oExCmd.StdOut
Do: WScript.Sleep 10
Do Until oExCmdStdOut.AtEndOfStream
CmdResults = CmdResults & oExCmdStdOut.ReadAll
Loop
Loop Until oExCmd.Status <> 0 and oExCmdStdOut.AtEndOfStream
End Function

-----------------------------------------------------------

Contents of patch_report.txt after the script runs:

Machine Name Product Bulletin Q Number Reason Status

FRODO (1**.***.***.***) WINDOWS XP GOLD All necessary
hotfixes have been applied. Information

FRODO (1**.***.***.***) INTERNET EXPLORER 6 GOLD
All necessary hotfixes have been applied. Information


Jeff Wright

Larry Bredehoeft

unread,
Jul 2, 2002, 4:07:50 PM7/2/02
to
Here's how I do it:

Set exec = WshShell.Exec( exec_str )
Do While exec.Status = 0
thd.Sleep( 150 )
Loop

result = exec.StdOut.ReadAll

------------------
(I have an ActiveX library I build that does the sleep).


"Jeff Wright" <jsw...@yahoo.com> wrote in message
news:q7p3iu0v033h3q874...@4ax.com...

alex_k._angelopoulos_(mvp)

unread,
Jul 2, 2002, 8:23:54 PM7/2/02
to
That's exactly the standard way of doing it - and was not possible
before WSH 5.6, since it introduced the Exec method, by the way.

There are other ways, also. I use the following 2 functions for clean
capture of output without having a visible console window. The first
captures the standard output of an console command and returns that as
the results; the 2nd actually returns a 3-element array, with the
following in each element

0 - the "as executed" command
1 - standard out
2 - standard error.


'============================================


Function Cmd(cmdline)
' Wrapper for getting StdOut from a console command
Dim Sh, FSO, fOut, OutF, sCmd
Set Sh = createobject("WScript.Shell")
Set FSO = createobject("Scripting.FileSystemObject")
fOut = FSO.GetTempName
sCmd = "%COMSPEC% /c " & cmdline & " >" & fOut
Sh.Run sCmd, 0, True
If FSO.FileExists(fOut) Then
If FSO.GetFile(fOut).Size>0 Then
Set OutF = FSO.OpenTextFile(fOut)
Cmd = OutF.Readall
OutF.Close
End If
FSO.DeleteFile(fOut)
End If
End Function

Function CaptureConsole(sCmd)
'returns as-run command, StdOut and StdErr in an array
Dim oSh, FSO, fErr, sData, fOut, ErrF, OutF, Cmd
Set oSh = createobject("WScript.Shell")
Set FSO = createobject("Scripting.FileSystemObject")
fOut = FSO.GetTempName
fErr = FSO.GetTempName
Cmd = "%COMSPEC% /c " & sCmd _
& " 2>" & fErr & " 1>" & fOut
oSh.Run Cmd, 0, True
If FSO.FileExists(fOut) Then
If FSO.GetFile(fOut).Size>0 Then
Set OutF = FSO.OpenTextFile(fOut)
sOut = OutF.Readall
OutF.Close
End If
FSO.DeleteFile(fOut)
End If
If FSO.FileExists(fErr) Then
If FSO.GetFile(fErr).Size>0 Then
Set ErrF = FSO.OpenTextFile(fErr)
sErr = ErrF.Readall
ErrF.Close
End If
FSO.DeleteFile(fErr)
End If
CaptureConsole = Array(Cmd, sOut, sErr)
End Function

--
Please respond in the newsgroup. I've still got unread email from the
week Win95 was released, if that tells you anything.
http://www.bittnet.com/winremote
http://www.bittnet.com/scripting


"Jeff Wright" <jsw...@yahoo.com> wrote in message

news:hcs3iuk6cquortn14...@4ax.com...

Steve Fulton

unread,
Jul 3, 2002, 7:25:46 AM7/3/02
to
A word of warning about this; if your command generates a lot of output, it
could fill the StdOut buffer. Then the WshScriptExec object will wait for
you to remove some lines from the buffer by reading them. And since your
script doesn't read from the buffer until the command completes, it will
wait a loooong time.

--
Pick battles big enough to matter, small enough to win. -Jonathan Kozol

=-=-=
Steve
-=-=-

"Larry Bredehoeft" <lar...@u.washington.edu> wrote in message
news:aft15v$33l2$1...@nntp6.u.washington.edu...


> Here's how I do it:
>
> Set exec = WshShell.Exec( exec_str )
> Do While exec.Status = 0
> thd.Sleep( 150 )
> Loop
>
> result = exec.StdOut.ReadAll
>
> ------------------
> (I have an ActiveX library I build that does the sleep).
>
>
> "Jeff Wright" <jsw...@yahoo.com> wrote in message
> news:q7p3iu0v033h3q874...@4ax.com...
> > I just did a google search for info on this and got no clear answer.
> >
> > What is the best way to capture output from a command prompt command.
> >
> > I am trying to capture the output from hfnetchk.exe. A text file would
> > be fine. But I would like to remove a chuck of the output for my final
> > output file.
> >
> > Running "hfnetchk.exe -s 2 -o tab" on a machine named FRODO produces
> > the following output..of which I only want the section that starts
> > "Scanning FRODO until the Done scanning FRODO:

[snip]


alex_k._angelopoulos_(mvp)

unread,
Jul 3, 2002, 2:17:54 PM7/3/02
to
That's not an everyday problem...which makes it even worse when you do
encounter it, since you spend an hour or two trying to figure out what
the heck is wrong.

In theory, buffer capacity is about 32K I think, same as the actual
internal console buffer.

--
Please respond in the newsgroup. I've still got unread email from the
week Win95 was released, if that tells you anything.
http://www.bittnet.com/winremote
http://www.bittnet.com/scripting


"Steve Fulton" <cerbe...@hotmail.com> wrote in message
news:e$8GQZoICHA.1604@tkmsftngp12...

Michael Harris (MVP)

unread,
Jul 3, 2002, 3:46:32 PM7/3/02
to
Steve Fulton wrote:
> A word of warning about this; if your command generates a lot of
> output, it could fill the StdOut buffer. Then the WshScriptExec
> object will wait for you to remove some lines from the buffer by
> reading them. And since your script doesn't read from the buffer
> until the command completes, it will wait a loooong time.
>

Been there, done that ;-)...

Which is why I use the following template code:

set shell = createobject("wscript.shell")
sCmd = ""
set wsx = shell.exec(sCmd)
set wsxOut = wsx.stdout
sOutput = ""
do: wscript.sleep 10
do until wsxOut.atendofstream
sOutput = sOutput & wsxOut.readall
loop
loop until wsx.status <> 0 and wsxOut.atendofstream


--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--


0 new messages