Answered by:
VB Script to accept PC names in text file and get registry key value out of those systems

Question
-
Hi All,
My task: Create VB script to accept PC names from a text file, process each host name to get Registry key value of individual host name and finally export them to a text file or an excel sheet having registry key value against each pc.
I want to pull key value of LSFORCEHOST located in HKCU/Environment of each PC and export this value against each pc in a text or Excel file.
What I achieved: I am able to process against single pc name but failed to process 3000+ systems listed in a text file which I tried to process through VB script. I am not pro and learning the VBS and cant write complex code.
My VBS code written so far(It failed many time with issue):
Option Explicit
Const HKEY_CURRENT_USER = &H80000001
Dim oFSO, sFile, oFile, sText, strComputer,oReg, strKeyPath, strValueName, strValue, objFSO, objFile, strContents
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "test.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Do While Not oFile.AtEndOfStream
sText = oFile.ReadLine
If Trim(sText) <> "" Then
strComputer = sText
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "Environment"
strValueName = "LSFORCEHOST"
oReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\destination.txt",1, ForReading)
strContents = strValue
Set objFile = objFSO.OpenTextFile("C:\destination.txt",2, ForWriting)
objFile.Write strContents
objFile.Close
End If
Loop
oFile.Close
End IfI think when I tried to process each PC name from text file against registry key value, above code not working. As per my knowledge some issue in loop. Do I need to add FOR EACH feature?
Please help to correct it. Thanks in advance.
- Edited by Kiran Kr Tamang Monday, January 6, 2014 2:24 PM
- Moved by Bill_Stewart Tuesday, March 25, 2014 7:46 PM Abandoned
Monday, January 6, 2014 2:21 PM
Answers
-
Thanks for your reply. I am gettng "ACCESS DENIED" error in below line
" oReg.GetStringValue HKEY_CURRENT_USER,sKeyPath,sValueName,sValue".
Can we pull registry key value of HKey Current User of a remote system?
It is not possible to read that key remotely.
¯\_(ツ)_/¯
Thursday, January 9, 2014 8:55 AM
All replies
-
I think when I tried to process each PC name from text file against registry key value, above code not working. As per my knowledge some issue in loop. Do I need to add FOR EACH feature?
Please help to correct it. Thanks in advance.
Your code contains numerous errors. Furthermore you make things difficult for yourself by giving similar names to the objects used for your input and output. If you are a systems administrator then your best bet is to make an effort to learn the language instead of grabbing a few code fragments from here and there and hoping that somehow they will work.
Try the cleaned up code below for this particular project. Note the consistent code indentation. It helps you enormously when trying to understand the structure of the code. Remember also that you must fully qualify all file paths. Writing to "Destination.txt" will cause endless confusion because you never know for sure where this file will reside.
Const HKEY_CURRENT_USER = &H80000001
Set oFSO = CreateObject("Scripting.FileSystemObject")
sKeyPath = "Environment"
sValueName = "LSFORCEHOST"
sFile = "D:\Test.txt"
If oFSO.FileExists(sFile) Then
Set oInput = oFSO.OpenTextFile(sFile, 1)
Do While Not oInput.AtEndOfStream
sComputer = oInput.ReadLine
If Trim(sComputer) <> "" Then
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\default:StdRegProv")
oReg.GetStringValue HKEY_CURRENT_USER,sKeyPath,sValueName,sValue
Set oOutput = oFSO.OpenTextFile("D:\destination.txt",8, True)
oOutput.WriteLine sValue
oOutput.Close
End If
Loop
oInput.Close
End If- Edited by Frederik Long Monday, January 6, 2014 3:50 PM
- Proposed as answer by Frederik Long Friday, January 17, 2014 8:54 PM
Monday, January 6, 2014 3:49 PM -
Thanks for your reply. I am gettng "ACCESS DENIED" error in below line
" oReg.GetStringValue HKEY_CURRENT_USER,sKeyPath,sValueName,sValue".
Can we pull registry key value of HKey Current User of a remote system?
Thursday, January 9, 2014 5:46 AM -
If you are getting an error for the line
oFSO.OpenTextFile("D:\destination.txt",8, True)
then drive D: is probably a CD drive.
Thursday, January 9, 2014 7:17 AM -
Thanks for your reply. I am gettng "ACCESS DENIED" error in below line
" oReg.GetStringValue HKEY_CURRENT_USER,sKeyPath,sValueName,sValue".
Can we pull registry key value of HKey Current User of a remote system?
It is not possible to read that key remotely.
¯\_(ツ)_/¯
Thursday, January 9, 2014 8:55 AM