[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-06-19
7
Medium Priority
?
13,286 Views
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
Loop

 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"
Else
   Wscript.Echo "Unknown OS"
End If


End Function
0
Comment
Question by:TristinColby
  • 3
  • 3
7 Comments
 
LVL 1

Author Comment

by:TristinColby
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
Loop
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?
0
 
LVL 1

Author Comment

by:TristinColby
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?
0
 
LVL 30

Expert Comment

by:callrs
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:callrs
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...
0
 
LVL 1

Author Comment

by:TristinColby
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()
Next
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
Else
   Wscript.Echo "Unknown OS"
   GetOS_Local = FALSE
   Wscript.Quit
End If


End Function
0
 
LVL 30

Expert Comment

by:callrs
ID: 16933548
The counter idea worked :-D
0
 
LVL 2

Accepted Solution

by:
TiwariVikas earned 1500 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.

Regards,
Vikas.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
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 …
Starting up a Project

872 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