idiot3qu3
asked on
vbscript to merge REG into remote registry problem
I am trying to add a REG file to all the machines in an OU via vbscript. This script will check a folder every 10 seconds to see if a new REG file has been created there. If it has, it will then go through the OU and merge the new REG file in to the remote machine registry.
The problem I'm having is that the script will only merge the REG file into the registry of the local machine the script is being run on and not to any other machine. Can someone take a look at this code to see what the problem is?
From what I can tell, the objService.Create ('regedit.exe /s " & strnFileName) is running on the local machine, but not on any other machine even though the script drills through the OU correctly.
Here is the code:
'This script checks a directory for new files
'then uses regedit.exe to merge a new registy
'file into the registries of computers in
'a given OU in AD
Const ADS_SCOPE_SUBTREE = 2
On Error Resume Next
'This portion is set to run on the host computer where a new file will be created
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'Checks on a certain interval of time for a creation event.
Set colMonitoredEvents = objWMIService.ExecNotifica tionQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile ' and " _
& "TargetInstance.GroupCompo nent= " _
& "'Win32_Directory.Name=""c :\\\\scrip ts""'")
'Loop to control what happens after an event is triggered
Do
Set objLatestEvent = colMonitoredEvents.NextEve nt
strNewFile = objLatestEvent.TargetInsta nce.PartCo mponent
arrNewFile = Split(strNewFile, "=")
strFileName = arrNewFile(1)
strFileName = Replace(strFileName, "\\", "\")
strFileName = Replace(strFileName, Chr(34), "")
strFilename = Replace(strFilename, "c:", "\\gpotest")
'Connects to AD LDAP
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio n = objConnection
objCommand.CommandText = _
"Select Name From 'LDAP://OU=BLAH,DC=BLAH,DC =COM' Where objectClass='computer'"
objCommand.Properties("Pag e Size") = 1000
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'This loop drills through AD OU to make sure every object is hit
Do Until objRecordSet.EOF
strComputer = objRecordSet.Fields("Name" ).Value
Set objShell = CreateObject("Wscript.Shel l")
Set objService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!//" & strComputer & "\root\cimv2:Win32_Process ")
WScript.Echo strComputer & strFileName
objService.Create("regedit .exe /s " & strFileName)
objRecordSet.MoveNext
Loop
Loop
------
Thanks for your help!
-Doug
The problem I'm having is that the script will only merge the REG file into the registry of the local machine the script is being run on and not to any other machine. Can someone take a look at this code to see what the problem is?
From what I can tell, the objService.Create ('regedit.exe /s " & strnFileName) is running on the local machine, but not on any other machine even though the script drills through the OU correctly.
Here is the code:
'This script checks a directory for new files
'then uses regedit.exe to merge a new registy
'file into the registries of computers in
'a given OU in AD
Const ADS_SCOPE_SUBTREE = 2
On Error Resume Next
'This portion is set to run on the host computer where a new file will be created
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'Checks on a certain interval of time for a creation event.
Set colMonitoredEvents = objWMIService.ExecNotifica
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile
& "TargetInstance.GroupCompo
& "'Win32_Directory.Name=""c
'Loop to control what happens after an event is triggered
Do
Set objLatestEvent = colMonitoredEvents.NextEve
strNewFile = objLatestEvent.TargetInsta
arrNewFile = Split(strNewFile, "=")
strFileName = arrNewFile(1)
strFileName = Replace(strFileName, "\\", "\")
strFileName = Replace(strFileName, Chr(34), "")
strFilename = Replace(strFilename, "c:", "\\gpotest")
'Connects to AD LDAP
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio
objCommand.CommandText = _
"Select Name From 'LDAP://OU=BLAH,DC=BLAH,DC
objCommand.Properties("Pag
objCommand.Properties("Sea
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'This loop drills through AD OU to make sure every object is hit
Do Until objRecordSet.EOF
strComputer = objRecordSet.Fields("Name"
Set objShell = CreateObject("Wscript.Shel
Set objService = GetObject("winmgmts:{imper
WScript.Echo strComputer & strFileName
objService.Create("regedit
objRecordSet.MoveNext
Loop
Loop
------
Thanks for your help!
-Doug
ASKER
Would this method mean that I would have to include the keys I want to add in the script?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys! This looks like the best way to do it. I had used PStools before, but the thought never crossed my mind to use it this way.
Remind me to bonk my head on my keyboard the next time I forget :-)
-Doug
Remind me to bonk my head on my keyboard the next time I forget :-)
-Doug
ASKER
Something's a little funky with the strCommand line...
I keep getting an "Expected end of Statement" when I try to run it. When I parse it, it looks like it has all the correct amount of quotes...?
-Doug
I keep getting an "Expected end of Statement" when I try to run it. When I parse it, it looks like it has all the correct amount of quotes...?
-Doug
ASKER
NM...I found it. There was a loose parenthesis at the end.
Thanks again!
-Doug
Thanks again!
-Doug
Great, no problem. Thanks for the grade,
Rob.
Rob.
Something like this could get you started:
'===========
Const HKEY_LOCAL_MACHINE = &H80000002
strRegBranch = "SOFTWARE\Microsoft\Window
strComputer = "."
Set objRegistry = GetObject("winmgmts:" & _
"{impersonationLevel=Imper
strComputer & "\root\default:StdRegProv"
'Returns a non-zero return value if the key does not exist
If objRegistry.EnumKey(HKEY_L
For intValueCount = LBound(arrValueNames) To UBound(arrValueNames)
MsgBox arrValueNames(intValueCoun
Next
End If
'=============
I can help you further tomorrow.....I'll spend more time looking at it.
Regards,
Rob.