Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 13548
  • Last Modified:

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

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
TristinColby
Asked:
TristinColby
  • 3
  • 3
1 Solution
 
TristinColbyAuthor Commented:
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
 
TristinColbyAuthor Commented:
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
 
callrsCommented:
Shouldn't you use this outside the loop:
Dim Results

?
Or why not just use strResult, which you have a Dim for earlier?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
callrsCommented:
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
 
TristinColbyAuthor Commented:
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
 
callrsCommented:
The counter idea worked :-D
0
 
TiwariVikasCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now