Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirecting stdout using run command

Status
Not open for further replies.

LeighTT

Technical User
Feb 12, 2003
15
GB
I'm having trouble capturing stdout from the run command using script below any ideas????

const ForAppending = 8, forwriting = 2
Set objShell=CreateObject("WScript.Shell")
Set objfso=CreateObject("Scripting.FileSystemObject")
set stdout = wscript.stdout

Set X = objshell.run ("%comspec% /c Reg query ""HKLM\software\microsoft\Windows nt\currentversion""")
Do While X.Status = 0
WScript.Sleep 100
Loop
Do While Not X.StdOut.AtEndOfStream
sLine = X.StdOut.Readline
WScript.Echo sline
If InStr(sline, "currentversion") Then
x = sline
WScript.Echo x
Else
End if
loop
 
LeighTT

[1] It is .exec not .run here. Also, get rid of %"comspec% /c", you don't need it here.
[tt]
Set X = objshell.exec ("Reg query ""HKLM\software\microsoft\Windows nt\currentversion""")
[/tt]
[2] A minor improvement might be to make the search of the signature string case insensitive.
[tt]
if InStr(1,sline,"currentversion",1) Then
[/tt]
Then you don't have to worry about the cases.

regards - tsuji
 
Also I overlooked this.
[3] You assign x to two completely different things, variable type. This certainly gives you runtime error. So instead of x call it y.
[tt]
[red]y[/red] = sline
WScript.Echo [red]y[/red]
[/tt]
- tsuji
 
When I substitue the RUN with EXEC command the program gets stuck in the loop below and gives no output.

Do While X.Status = 0
WScript.Sleep 100
Loop
 
>When I substitue the RUN with EXEC command the program gets stuck in the loop below and gives no output.

What is the line exactly after you substitute run?
 
I've changed it to a simplified version as below, if I remove the do while loop then output is ok. The oExec status always remains 0

Dim x
const ForAppending = 8, forwriting = 2
Set objShell=CreateObject("WScript.Shell")
Set objfso=CreateObject("Scripting.FileSystemObject")
set stdout = wscript.stdout


Set oExec = objshell.exec ("Reg query ""HKLM\software\microsoft""")

WScript.Echo oExec.status
Do While oExec.Status = 0
Wscript.Sleep 100
WScript.Echo oExec.status
Loop
WScript.Echo oExec.status
sLine = oExec.StdOut.Readall

 
Do you need this somewhere?
>set stdout = wscript.stdout
Try get rid of this line.

Else, I don't think it would remain 0 whereas you say it works even without the looping.

- tsuji
 
I have substitued the command line with

Set oExec = objshell.exec ("calc.exe")

and this works fine, so it seems to be something related to the Reg.exe command line not terminating correctly any ideas as i'm stumped
 
LeighTT,

I ran this code, and it worked for me:
Code:
Dim x
const ForAppending = 8, forwriting = 2
Set objShell=CreateObject("WScript.Shell")
Set objfso=CreateObject("Scripting.FileSystemObject")
'set stdout = wscript.stdout
        
Set oExec = objshell.exec ("Reg query ""HKLM\software\microsoft""")

'WScript.Echo oExec.status
Do Until oExec.Status = 0
	Wscript.Sleep 100
	WScript.Echo oExec.status
Loop

WScript.Echo oExec.status
sLine = oExec.StdOut.Readall
WScript.Echo sLine

Note the only changes I made to your script that matter are in the loop and the last line. Change 'Do While' to 'Do Until'. That will take care of your infinite loop. I was able to successfully display the information with the last line the same way I saw output when I ran the command in a command line window.

Good luck!
 
Do until status = 0 would imply continue before the command has completed

WshRunning ( = 0)
The job is still running.
WshFinished ( = 1)
The job has completed

My script needs to confirm the command has completed before moving on and there lies the problem when using the req.exe command.
 
LeighTT,

I have a preference to suppressing %comspec% /c as far as possible for .exec. But as there are unexpected problem, could you try re-insert it back see if it corrects any problem? I don't like it, but it might help...

Try this first, without quotes as they are not needed.
[tt]
Set oExec = objshell.exec ("Reg query HKLM\software\microsoft")
[/tt]
If still have problem, try re-insert the %comspec% /c.
[tt]
Set oExec = objshell.exec ("%comspec% /c Reg query HKLM\software\microsoft")
[/tt]
- tsuji

 
I have tested both of your suggestions but the problem persists.
 
Oh, sorry. I guess I was under the impression that 0 always meant successful, my bad :). However, here is something else that you can try...
Code:
Option Explicit

Dim objReg
Dim strKeyPath, strComputer, strReturn
Dim aryKeys
Dim i

'Subtrees
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005

strComputer = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "software\microsoft"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, aryKeys
 
For i = 0 To UBound(aryKeys)
	strReturn = strReturn & aryKeys(i) & vbCrlf 
Next

MsgBox strReturn

Good luck!
 
The machines I need to query do not have WMI installed and its not possible to role out WMI within timeframe hence the use of Reg.exe.
 
The script is running on XP however the registry query will be across NT/2000 & 2003
 
LeighTT,
What happens when you issue the commandline in the cmd prompt window? I guess you are not asked to be interactive in your case? You know, if you do it in a batch file, you can as well monitor the return errorlevel. In any case, it would end the query by itself without you intervening?
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top