Page 1 of 1 1
Topic Options
#145228 - 2005-08-10 06:34 PM Rename sAMAccountName of group using fnLDAPQuery and groupRename functions
Scriptodude Offline
Fresh Scripter

Registered: 2005-04-22
Posts: 26
Help please!!!!

Guys,

Can anyone tell me how to modify the sAMAccountName of a group inn KIX? I am currently using the fnLDAPQuery(created by Chris S.) to pull info from AD and then the function below to rename the RDN of the group. This does not change the sAMAccountName though. I beleive this is because the MoveHere function can only change the RDN but I am not certain. Any help would be appreciated. The groupRename function is a mod of the userRename function created by Bryce(I think?).

Thanks,
Scripto

Code:
   
;Function to rename a group in AD!
Function groupRename($target, $oldgroup, $newgroup)
DIM $computer, $computerOBJ, $groupOBJ, $nul, $target
$TargetOBJ = getobject("LDAP://$target")
if vartype($TargetOBJ) <> 9 exit(@error) endif
$groupOBJ = getobject("LDAP://")
if vartype($groupOBJ) <> 9 exit(@error) endif
$nul = $TargetOBJ.MoveHere($groupOBJ.adspath,$newgroup)
exit(@error)
Endfunction



Edited by Scriptodude (2005-08-10 08:30 PM)

Top
#145229 - 2005-08-10 06:50 PM Re: Rename sAMAccountName of group using fnLDAPQuery and groupRename functions
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Howsabout...

Code:

$objGroup = GetObject("LDAP://cn=MyGroup,ou=GROUPS,dc=my,dc=domain,dc=com")

$objGroup.Put("sAMAccountName","MyNewName")
$objGroup.SetInfo


Top
#145230 - 2005-08-10 07:59 PM Re: Rename sAMAccountName of group using fnLDAPQuery and groupRename functions
Scriptodude Offline
Fresh Scripter

Registered: 2005-04-22
Posts: 26
Chris,

Worked like a charm! I just had to put your code in front of the MoveHere function due to the delay of the move/rename.

Thanks for the help Chris!

Bryan

Top
#145231 - 2005-08-10 08:04 PM Re: Rename sAMAccountName of group using fnLDAPQuery and groupRename functions
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Glad to help.
Top
#145232 - 2005-08-11 03:48 PM Re: Rename sAMAccountName of group using fnLDAPQuery and groupRename functions
Scriptodude Offline
Fresh Scripter

Registered: 2005-04-22
Posts: 26
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


Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1046 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.053 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org