Link to home
Start Free TrialLog in
Avatar of idiot3qu3
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.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\scripts""'")

'Loop to control what happens after an event is triggered

Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    strNewFile = objLatestEvent.TargetInstance.PartComponent
    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.Connection")
    Set objCommand =   CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"

    Set objCommand.ActiveConnection = objConnection
    objCommand.CommandText = _
    "Select Name From 'LDAP://OU=BLAH,DC=BLAH,DC=COM' Where objectClass='computer'"  
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = 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.Shell")
       Set objService = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "\root\cimv2:Win32_Process")
       WScript.Echo strComputer & strFileName
       objService.Create("regedit.exe /s " & strFileName)
       objRecordSet.MoveNext
    Loop
Loop
------

Thanks for your help!

-Doug
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, I just had a quick look at this, and it looks like you might be better off using StdRegProv, instead of the process.

Something like this could get you started:
'===========
Const HKEY_LOCAL_MACHINE = &H80000002
strRegBranch = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"

strComputer = "."

Set objRegistry = GetObject("winmgmts:"   & _
      "{impersonationLevel=Impersonate}!\\" & _
      strComputer & "\root\default:StdRegProv")

'Returns a non-zero return value if the key does not exist
If objRegistry.EnumKey(HKEY_LOCAL_MACHINE, strRegBranch, arrValueNames) = 0 Then
   For intValueCount = LBound(arrValueNames) To UBound(arrValueNames)
      MsgBox arrValueNames(intValueCount)
   Next
End If
'=============

I can help you further tomorrow.....I'll spend more time looking at it.

Regards,

Rob.
Avatar of idiot3qu3
idiot3qu3

ASKER

Would this method mean that I would have to include the keys I want to add in the script?
SOLUTION
Avatar of Vorenus
Vorenus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
NM...I found it.  There was a loose parenthesis at the end.

Thanks again!

-Doug
Great, no problem.  Thanks for the grade,

Rob.