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
12,963 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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.

Regards,
Vikas.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 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