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?
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This article will show, step by step, how to integrate R code into a R Sweave document
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now