In case anyone is interested here is the code I used to append a name to the front of all the groups in a container. I filtered groups that already had the appended name using the fnLDAPQuery function. The messagebox portion of the script can be used if you want to be told what group has been renamed. The script is definately not perfect and could use a little cleaning up but it worked for me.
Bryan
Code:
Dim $What,$sADsPath,$strFilter
Dim $aResults,$c,$r,$append
$What = "sAMAccountName" ;What attributes do we want to use in the fnLDAPQuery function?
$From = "LDAP://your ldap path"
$sADsPath = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext") ;Sets variable to root of the domain
$strFilter = "(&(objectCategory=group)(!sAMAccountName=Anything*)(!sAMAccountName=Name-*))" ;Sets variable to filter on the class/attribute you want to find in AD!
;$msg = messagebox("This script will append 'Name-' to all group's in the $Working OU! If you select YES the first group found will be renamed? Would you like to continue and rename the groups ?","GROUP RENAME SCRIPT",4)
; IF $msg = 7 goto "end"
; EndIf
$aR = fnLDAPQuery($What, $From, $strFilter) ;Start the fnLDAPQuery function using the variables listed above!
For $c = 0 to Ubound($aR)
For $r = 0 to UBound($aR,2)
If VarType($aR[$c,$r])<8192 ;Attribute is not binary?
$append = "Name-" ;Set the name to append to the old group name for use with groupRename!
$oldgroup = $aR[$c,$r] ;Set the oldgroup variable to the current group that is not named correctly!
$newgroup = $append + $oldgroup ;Add Name- to the beginning of the oldgroup name!
$rname = groupRename($target, $oldgroup, $newgroup) ;Start the groupRename function using the variables listed above!
EndIf
Next
?
Next
Function fnLDAPQuery($What,Optional $From,Optional $Filter,Optional $OrderBy,Optional $Scope,
Optional $User,Optional $Pswd)
Dim $oCon,$oCMD,$oRS,$sQ,$aR,$C,$R
$sQ="<"+Iif($From="","LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext"),$From)+">;"+$Filter+";"+Iif(VarType($What)>8192,Join($What,','),$What)+";"+Iif($Scope<>"base" AND $Scope<>"onelevel","subtree",$Scope)
$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Properties("Encrypt Password").Value=1
$oCon.Properties("ADSI Flag").Value=1
If $User AND $Pswd
$oCon.Properties("User ID").Value=$User
$oCon.Properties("Password").Value=$Pswd
EndIf
$oCon.Open("Active Directory Provider")
$oCMD=CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Cache Results").Value=0
If InStr($OrderBy,"distinguishedName")
$oRS=CreateObject("ADODB.Recordset")
$oRS.CursorLocation=3
$oRS.Sort=$OrderBy
$oRS.Open($sQ,$oCon,0,1,1)
Else
If $OrderBy
$oCMD.Properties("Sort On").Value=$OrderBy
EndIf
$oRS=$oCMD.Execute
EndIf
If @ERROR Exit @ERROR EndIf
If $oRS.BOF AND $oRS.EOF Exit @ERROR EndIf
$aR = $oRS.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
Next
Next
$fnLDAPQuery=$aFR
EndFunction
;Function to rename a group in AD!
Function groupRename($target, $oldgroup, $newgroup)
DIM $computer, $computerOBJ, $groupOBJ, $nul, $target
$TargetOBJ = getobject("$From") ;What OU am I in?
if vartype($TargetOBJ) <> 9 exit(@error) endif ;Am I the correct variable type?
$groupOBJ = getobject("LDAP://CN=$oldgroup,your ldap path") ;What group object needs to be renamed?
if vartype($groupOBJ) <> 9 exit(@error) endif ;Am I the correct variable type?
$groupOBJ.Put("sAMAccountName","$newgroup") ;Rename the group sAMAccountName!
$groupOBJ.SetInfo
; $nul = $TargetOBJ.MoveHere($groupOBJ.adspath,$newgroup) ;Rename the group CN/RDN!
$oldgroup ?
; If @ERROR <> "-2147352567" ;If an error occurs display the failed message box!
; $msg = messagebox("SCRIPT FAILED TO RENAME $oldgroup WITH AN ERROR OF '@SERROR'! Would you like to try and rename the next group?","GROUP RENAME ERROR",4)
; IF $msg = 7 goto "end"
; EndIf
; Else
; IF @ERROR = "-2147352567" ;If there are no errors display the success message box!
; $msg = messagebox("$oldgroup has been renamed to $newgroup! Would you like to rename the next group?","GROUP RENAME SUCCESS",4)
; IF $msg = 7 goto "end"
; EndIf
; EndIf
; EndIf
exit(@error)
Endfunction
:end
exit