Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remote reg query vbscript

Status
Not open for further replies.

mrb83

IS-IT--Management
May 18, 2010
4
US
Hello all,

I am trying to run a vbscript to run a reg query command on remote systems. I am passing a list of system names into the script from a text file. The script runs and is reading the text file fine but the output is only writing the name of the system in the output text file, not the output of the reg query command. I have searched this forum, searched the Internet/Googled for a while and cannot find the reason why this is not working. This is what I have so far:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objTextFile = objFSO.OpenTextFile("C:\workstations.txt", ForReading)
Const REGQ = "C:\REG QUERY "
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.ReadLine
strRegKey="HKLM\Software\Policies\Microsoft\Windows\NetCache\"
Set objExec = objShell.Exec("cmd /c " & REGQ & " \\" & strComputer & strRegKey)
Do While Not objExec.StdOut.Atendofstream
val = (objExec.StdOut.ReadAll)
Loop
Set fs = CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile("C:\offline-regcheck.txt", 8, True)
file.Write ""& strComputer & vbCrLf
file.Write "" & val & vbCrLf
file.close
Loop
objTextFile.Close
wscript.echo "Done"
wscript.quit

Any pointers/tips would be greatly appreciated.
 
two things stick out.

1. Unless the strComputer has a space as its last character, I don't think this command works. Add a space between strComputer & strRegKey.
Code:
Set objExec = objShell.Exec("cmd /c " & REGQ & " \\" & strComputer & [red] " " & [/red] strRegKey)

2. If you are going to .ReadAll, do you really need a loop?
Code:
Do While Not bjExec.StdOut.Atendofstream
   val = (objExec.StdOut.ReadAll)
loop

-Geates
 
Thanks for the reply. I put in the space as you indicated but the output still just lists the machine name. I also removed the extra uneeded loop.

Thanks again for the help .
 
do a cmd /k, not a cmd /c, at least for testing you will be able to see the result of you command line...once you know you have the syntax right and you are getting somethign nice to the screen in the DOS box you can worry about stuffing it into an out put file.

btw, >> might be an easier way to redirect your output ;-)
 
Thanks for the response. I changed cmd /c to cmd /k and the reg query command is definitely not running. The DOS box just come up blank with a flashing curser. Does anyone know if the syntax is correct for the script? The command

reg query \\machine\HKLM\Software\Policies\Microsoft\Windows\NetCache

runs and prints out the desired output outside of the script at a command prompt with no issues.
 
I'd replace this:
Const REGQ = "C:\REG QUERY "
with this:
Const REGQ = "REG QUERY "

this:
strRegKey="HKLM\Software\Policies\Microsoft\Windows\NetCache\"
with this:
strRegKey="HKLM\Software\Policies\Microsoft\Windows\NetCache"

and this:
Set objExec = objShell.Exec("cmd /c " & REGQ & " \\" & strComputer & strRegKey)
with this:
Set objExec = objShell.Exec("cmd /c " & REGQ & " \\" & strComputer & "\" & strRegKey)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Those last edits did the trick! :)
Thank you everyone for the pointers it is very much appreciated!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top