VBScript Reading command line program output from StdOut using exec and storing it for parsing, HOW DO I??

Posted on 2006-06-19
Last Modified: 2012-08-14
I am trying to read the output of  a reg query command but I can't seem to figure out how to store the data so I can use InStr() on it.  Here's the code.

' Remote try on US Domain
X = GetOS_Local("Computer","adminName","password")
Wscript.Echo X

Function GetOS_Local(strComputerName, strAdminName, strAdminPass) ' Return True if OS is 2k or XP
Dim objMyShell
Dim objExecuteCode
Dim objStdOut
Dim strResult
Dim OSxp, OS2k
Dim strCmd_NetUse, strCmd_NetUseDel, strCmd_Rcmd, strCmd_Copy, strCmd_DelOldLog

OSxp = LCase("Microsoft Windows XP Professional")
OS2k = LCase("Microsoft Windows 2000 Professional")

strCmd_NetUse = ("net use \\" & strComputerName & "\ipc$ /u:localhost\" _
                             & strAdminName & " " & strAdminPass)

strCmd_NetUseDel = ("net use /d \\" & strComputerName & "\ipc$")

strCmd_RegQuery = ("%comspec% /c reg query " & chr(34) & "\\"  & strComputerName & "\HKLM\Software\Microsoft\Windows NT\CurrentVersion" & chr(34) & " /v ProductName")

Set objMyShell = CreateObject("Wscript.Shell")

Set objExecuteCode = objMyShell.Exec(strCmd_NetUseDel) 'Delete any Net use to box in place
Wscript.Echo strCmd_NetUseDel

Set objExecuteCode = objMyShell.Exec(strCmd_NetUse) ' Net use to box with clean connection
Wscript.Sleep(1000) ' MUST have it here so net use has time to complete its operation
Wscript.Echo strCmd_NetUse

Set objExecuteCode = objMyShell.Exec(strCmd_RegQuery)
'For Error Checking Purposes Only objMyShell.Run("%comspec% /k" & strCmd_RegQuery)

' --------- Begin PROBLEM CODE --------------

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(strCmd_RegQuery)

Do Until objExecObject.StdOut.AtEndOfStream    ' I WANT TO STORE THIS DATA So I CAN PARSE IT BELOW
 Wscript.Echo objExecObject.StdOut.ReadLine()  
results = objExecObject.StdOut.Readline() ' Won't work. No output is caught
Wscript.Echo "Here are the results: " & results

 f InStr(1,Results, "xp",1) Then  ' Results is the string I am trying to store the data into.
   Wscript.Echo "XP"
ElsIf InStr(1,Results, "2000",1)
   Wscript.Echo "2000"
   Wscript.Echo "Unknown OS"
End If

End Function
Question by:TristinColby
  • 3
  • 3

Author Comment

ID: 16933333
New Twist. Here is some updated code

' --------- Begin Main Code --------------

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(strCmd_RegQuery)

Do Until objExecObject.StdOut.AtEndOfStream
 'Wscript.Echo objExecObject.StdOut.ReadLine()
 Results = objExecObject.StdOut.ReadLine() ' In the Do loop Results is correct
 Wscript.Echo results
Wscript.Echo results 'Outside the Do loop its empty

I NEED TO KEEP THIS STRING SO I can work with it later on. Why isn't it holding its value?

Author Comment

ID: 16933374
I just figured out why the return value is nothing. Its because the do loop keeps going until it hits nothing. So how can i store this value on 1st run only?
LVL 30

Expert Comment

ID: 16933388
Shouldn't you use this outside the loop:
Dim Results

Or why not just use strResult, which you have a Dim for earlier?
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 30

Expert Comment

ID: 16933407
Use a counter, store Results into a separate variable when counter is 0.

OR you can use a string array to hold all the results...

Author Comment

ID: 16933505
Nevermid I figured it out. Here's the corrected code.
' ----- Begin Main Code -------------
Dim results

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(strCmd_RegQuery)

For i=1 to 4
 Results = objExecObject.StdOut.ReadLine()
Results = objExecObject.Stdout.Readline() 'Skipped ahead so we could read the important string

If InStr(1,Results, "xp",1) Or InStr(1,Results, "2000",1) Then
   Wscript.Echo "XP or 2000"
   GetOS_Local = TRUE
   Wscript.Echo "Unknown OS"
   GetOS_Local = FALSE
End If

End Function
LVL 30

Expert Comment

ID: 16933548
The counter idea worked :-D

Accepted Solution

TiwariVikas earned 500 total points
ID: 16934915
Hi TristinColby ,

Try this....

Results = Results & objExecObject.StdOut.ReadLine()

to get all the output.

Else you can use....

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(strCmd_RegQuery)
Results = objExecObject.StdOut.ReadAll()

Your above approach will work only if the required text (xp or 2000) is in the last line of output and may fail in other cases.


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
word0 challenge 4 69
advertisement module in core php 4 164
tvirtualstringtree freeze when load too manny images 10 63
Using Third Party DLL with Access VBA 14 18
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question