Hello All,

Is there anyway for me to capture the output of the oShell.Run command and
assign it to a variable?

I want to run IPCONFIG /ALL and get the output dumps into a variable, so I
tried to do this but none of them work:

1. Output the command results to textfile.

Set oShell = createObject("WScript.Shell")
oShell.Run ("ipconfig /all > c:\output.txt")

2. Output the command resutlts to variable

Set oShell = createObject("WScript.Shell")
RetVal = oShell.Run ("ipconfig /all")

Also, what is the differences between oShell.Run and oShell.Exec

Thanks All,
Peter Nguyen

Re: Capture output from oShell.Run command??? by Torgeir

Torgeir
Tue Mar 02 18:30:21 CST 2004

Peter Nguyen wrote:

> Is there anyway for me to capture the output of the oShell.Run command and
> assign it to a variable?
>
> I want to run IPCONFIG /ALL and get the output dumps into a variable, so I
> tried to do this but none of them work:
>
> 1. Output the command results to textfile.
>
> Set oShell = createObject("WScript.Shell")
> oShell.Run ("ipconfig /all > c:\output.txt")

Hi

As it is the command processor that supports redirection, you need to add that
to your code:

oShell.Run ("%comspec% /c ipconfig /all > c:\output.txt")


Here is an example playing a bit with ipconfig's output:


Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

sExe = "ipconfig.exe /all"

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

oShell.Run "%comspec% /c " & sExe & " >" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

' get ipconfig's output into a variable.
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

' ipconfig's output contains some vbCr characters that can give
' some unwanted side effects, removing them using Replace.
sResults = Replace(sResults, vbCr, "")

' create an array of the output.
aResult = Split(sResults, vbCrLf)
For i = 0 To UBound(aResult)
WScript.Echo aResult(i)
Next




--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx



Re: Capture output from oShell.Run command??? by Peter

Peter
Tue Mar 02 22:26:20 CST 2004

Hello Torgeir,

Thank you very much. This is exactly what I'm looking for.

Out of my curiousity. Instead of writing the output to the file, then read
the file into a variable, can we capture the output directly to a variable?

Thanks again,
Peter Nguyen


"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:4045271C.FEA01A9A@hydro.com...
> Peter Nguyen wrote:
>
> > Is there anyway for me to capture the output of the oShell.Run command
and
> > assign it to a variable?
> >
> > I want to run IPCONFIG /ALL and get the output dumps into a variable, so
I
> > tried to do this but none of them work:
> >
> > 1. Output the command results to textfile.
> >
> > Set oShell = createObject("WScript.Shell")
> > oShell.Run ("ipconfig /all > c:\output.txt")
>
> Hi
>
> As it is the command processor that supports redirection, you need to add
that
> to your code:
>
> oShell.Run ("%comspec% /c ipconfig /all > c:\output.txt")
>
>
> Here is an example playing a bit with ipconfig's output:
>
>
> Const OpenAsASCII = 0
> Const FailIfNotExist = 0
> Const ForReading = 1
>
> sExe = "ipconfig.exe /all"
>
> Set oShell = CreateObject("WScript.Shell")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
> sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName
>
> oShell.Run "%comspec% /c " & sExe & " >" & sTempFile, 0 , True
>
> Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
> FailIfNotExist, OpenAsASCII)
>
> ' get ipconfig's output into a variable.
> sResults = fFile.ReadAll
> fFile.Close
> oFSO.DeleteFile(sTempFile)
>
> ' ipconfig's output contains some vbCr characters that can give
> ' some unwanted side effects, removing them using Replace.
> sResults = Replace(sResults, vbCr, "")
>
> ' create an array of the output.
> aResult = Split(sResults, vbCrLf)
> For i = 0 To UBound(aResult)
> WScript.Echo aResult(i)
> Next
>
>
>
>
> --
> torgeir
> Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of the 1328 page
> Scripting Guide:
> http://www.microsoft.com/technet/community/scriptcenter/default.mspx
>
>



Re: Capture output from oShell.Run command??? by Tom

Tom
Wed Mar 03 14:00:47 CST 2004

No, not with the Run method. However, the Exec method of
WSH v5.6 could be used to do that. The tradeoff is that
Exec MUST open a command prompt console window to do it.
Therefore, I personally use the Run method as in Togeir's
example.

For completeness, the Exec approach could be something
like this ...

sExe = "ipconfig.exe /all"
with CreateObject("WScript.Shell")
with .Exec(sExe)
with .StdOut
do until .AtEndofStream
s = s & Replace(.ReadLine, vbcr, "") _
& vbNewLine
Loop
end with ' StdOut
end with ' Exec
end with ' Shell
wsh.echo s

If you don't mind the console flash, you could use this
approach.

Tom Lavedas
===========

>-----Original Message-----
>Hello Torgeir,
>
>Thank you very much. This is exactly what I'm looking for.
>
>Out of my curiousity. Instead of writing the output to
the file, then read
>the file into a variable, can we capture the output
directly to a variable?
>
>Thanks again,
>Peter Nguyen
>
>
>"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com>
wrote in message
>news:4045271C.FEA01A9A@hydro.com...
>> Peter Nguyen wrote:
>>
>> > Is there anyway for me to capture the output of the
oShell.Run command
>and
>> > assign it to a variable?
>> >
>> > I want to run IPCONFIG /ALL and get the output dumps
into a variable, so
>I
>> > tried to do this but none of them work:
>> >
>> > 1. Output the command results to textfile.
>> >
>> > Set oShell = createObject("WScript.Shell")
>> > oShell.Run ("ipconfig /all > c:\output.txt")
>>
>> Hi
>>
>> As it is the command processor that supports
redirection, you need to add
>that
>> to your code:
>>
>> oShell.Run ("%comspec% /c ipconfig /all >
c:\output.txt")
>>
>>
>> Here is an example playing a bit with ipconfig's output:
>>
>>
>> Const OpenAsASCII = 0
>> Const FailIfNotExist = 0
>> Const ForReading = 1
>>
>> sExe = "ipconfig.exe /all"
>>
>> Set oShell = CreateObject("WScript.Shell")
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
>> sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" &
oFSO.GetTempName
>>
>> oShell.Run "%comspec% /c " & sExe & " >" & sTempFile,
0 , True
>>
>> Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
>> FailIfNotExist,
OpenAsASCII)
>>
>> ' get ipconfig's output into a variable.
>> sResults = fFile.ReadAll
>> fFile.Close
>> oFSO.DeleteFile(sTempFile)
>>
>> ' ipconfig's output contains some vbCr characters that
can give
>> ' some unwanted side effects, removing them using
Replace.
>> sResults = Replace(sResults, vbCr, "")
>>
>> ' create an array of the output.
>> aResult = Split(sResults, vbCrLf)
>> For i = 0 To UBound(aResult)
>> WScript.Echo aResult(i)
>> Next
>>
>>
>>
>>
>> --
>> torgeir
>> Microsoft MVP Scripting and WMI, Porsgrunn Norway
>> Administration scripting examples and an ONLINE version
of the 1328 page
>> Scripting Guide:
>>
http://www.microsoft.com/technet/community/scriptcenter/def
ault.mspx
>>
>>
>
>
>.
>

Re: Capture output from oShell.Run command??? by Torgeir

Torgeir
Wed Mar 03 14:38:36 CST 2004

Peter Nguyen wrote:

> Hello Torgeir,
>
> Thank you very much. This is exactly what I'm looking for.
>
> Out of my curiousity. Instead of writing the output to the file, then read
> the file into a variable, can we capture the output directly to a variable?

Hi

Yes, by using the Exec method.

Note, there is a couple of downsides with the Exec method:

First, you will need to have WSH 5.6 installed to be able to use it (and that
is not the case for a lot of computers in many environments).

Secondly, when running the script with wscript.exe, there is no way to hide the
command prompt that shows up.


It this is no problem for you, Exec is fine to use for this,
here are some examples:

http://groups.google.com/groups?selm=O9PW5osICHA.2088%40tkmsftngp11

http://groups.google.com/groups?selm=%23hg5V3ZzCHA.216%40TK2MSFTNGP12

http://groups.google.com/groups?selm=eWWmHqFiDHA.2400%40TK2MSFTNGP11.phx.gbl

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx