Link to home
Start Free TrialLog in
Avatar of charast
charastFlag for United States of America

asked on

VBscript to rename Groups in AD

I need to rename several hundred AD groups within an nested OU  (  ldap://OU=group,OU=group,dc=domain,dc=domain,dc=domain). I have the basic script to do the rename:
Set objOU = GetObject("LDAP://ou=Sales,dc=MyDomain,dc=com")
objOU.MoveHere "LDAP://cn=GroupA,ou=Sales,dc=MyDomain,dc=com", "cn=GroupB"

but I am unsure how to apply this to rename several groups in the target OU. The OU contains other groups which I dont want to change. in essence I need to wildcard certain groups to change.
i.e... I need to change all groups named abcde_servername1_*_*   to   abcde_servername2_*_*  
obviously the SID needs to remain. I need the SamAccountName (pre-win2k name) and the display name changed. All of the groups are standard Global Security groups.

Avatar of charast

ASKER

niether of those solutions work for me. the first has an input box to rename a specific group (one off)
and the second renames EVERY group in an OU.
I need to wildcard about 200 groups within an OU that has over 6000 groups in it.

any other suggestions out there?
Paste the script below into a text file with a .vbs extension.  Customize the value of the strContainer variable on line 3 with the distinguished name of the OU containing the groups.  Customize the value of the strOldPrefix variable on line 4 with the name prefix to find.  Customize the value of the strNewPrefix variable on line 6 with the new name prefix.

Running the script (e.g. with cscript scriptname.vbs) will echo the actions to be taken.  Once you have tested it successfully and are certain it will do what you intend, remove the apostrophe from line 36 to rename the groups.


Const ADS_SCOPE_ONELEVEL = 1
 
strContainer = "OU=group,OU=group,dc=domain,dc=domain,dc=domain"
strOldPrefix = "abcde_servername1_"
strChar = "_"
strNewPrefix = "abcde_servername2_"
 
intLen = Len(strOldPrefix)
strContainerPath = "LDAP://" & strContainer
Set objContainer = GetObject(strContainerPath)
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_ONELEVEL
 
objCommand.CommandText = _
    "SELECT ADsPath, Name FROM '" & strContainerPath & "' WHERE objectCategory='group'" 
 
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
    strADsPath = objRecordSet.Fields("ADsPath").Value
    strName = objRecordSet.Fields("Name").Value
    intPos = InStr(intLen + 1, strName, strChar)
    
    If LCase(Left(strName, intLen)) = LCase(strOldPrefix) And intPos > 0 Then
        strNewName = strNewPrefix & Mid(strName, intLen + 1)
        WScript.Echo "Rename " & strName & " to " & strNewName
        'objContainer.MoveHere strADsPath, "cn=" & strNewName
    End If
 
    objRecordSet.MoveNext
Loop

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of charast
charast
Flag of United States of America image

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