Page 1 of 1 1
Topic Options
#40823 - 2003-05-30 03:45 AM Changing the "common name" in AD
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Is it possible to change the common name in Active Directory via script? The problem that I have is a common name such as Doe, John T. is returned as cn=Doe\, John T.

The "\" causes some problems for some other scripts that we use. I can obviously change the name via AD users and computers by right clicking on it and choosing "rename" but I have many to change and would like to do it with a script. Before I get to carried away with the script, I wanted to try something simple just to test. Here's what I used:

code:
$target = GetObject("LDAP://ou=users,DC=blahblah,DC=com")

For Each $user in $target
If $user.name = "cn=Doe\, John T."
$newname = "cn=John T. Doe"
$user.name=$newname
$user.SetInfo
? 'Error = '+@ERROR+' - '+@SERROR
?"New name is: "$user.name
EndIf
Next

If I run this script, it will find cn listed above, but using user.SetInfo returns the following error: Error = -2147352562 - Invalid number of parameters. Any help would be greatly appreciated.

Top
#40824 - 2003-05-30 02:17 PM Re: Changing the "common name" in AD
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Come on....I know somebody out there has tried this before. At least tell me that it can't be done. [Smile] where's the love? [Smile]
Top
#40825 - 2003-05-30 02:46 PM Re: Changing the "common name" in AD
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
JohnQ, I saw your post last night and will be able to respond after doing some testing. Unfortunately, I will not have time until after work. If no one responds by then, look for my post this evening.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40826 - 2003-05-30 03:15 PM Re: Changing the "common name" in AD
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Thanks Howard. You always come through. I'll look for your post this evening.
Top
#40827 - 2003-05-30 07:32 PM Re: Changing the "common name" in AD
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I'm getting the same thing when I try it with my name. But I don't think it is necessarily a SetInfo issue, because I get the same error if i put an @serror after the assignment

code:
$user.name=$newname
@serror

And i haven't had any trouble changing other properties with the same script.
_________________________
Eric

Top
#40828 - 2003-05-31 01:07 AM Re: Changing the "common name" in AD
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 622
Loc: London, England
$STRUserName would be your users UserID

TranslateName is a Function.

Common Name, would be CN, so $ADSUser.Put ("CN", $CommonName)

code:
 $TranslateUserName = TranslateName (3, "", 3, "@Domain\$STRUserName", 1)
$ADSUser = GetObject("LDAP://" +$TranslateUserName[0])

$ADSUser.Put ("givenName", $GivenName)
$ADSUser.Put ("sn", $SN)
$ADSUser.Put ("Displayname", $DisplayName)
$ADSUser.Put ("comment", $DOB)
$adsUser.HomeDirectory = "$HomePath\$newUserName\My Documents"
$ADSUser.Put ("samAccountName", $NewUsername)
$ADSUser.SetInfo

There you go, pick the bones out of that.

[ 31. May 2003, 01:08: Message edited by: Richard Farthing ]

Top
#40829 - 2003-05-31 01:14 AM Re: Changing the "common name" in AD
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
JohnQ, the error your are receiving is not from the SetInfo but from the attempt to improperly set the name attribute.

The code below does not get your error but instead returns:
quote:

0 The operation completed successfully.
-2147352567 COM exception error "SetInfo" ((null) - (null)) [-2147352567/80020009]

Error: 9
The storage control block address is invalid.

code:
$DN = TranslateName (1, @domain, 3, "tycoelectronics\us035355", 1)
? $DN[0]

$obj= GetObject("LDAP://@domain/"+$DN[0])
? $obj.Name
$obj.Put("Name","US035355A")
; I also tried $obj.Put("CN","US035355A")

? "@error @serror"
$obj.SetInfo
? "@error @serror"

? ConvertCOMerror(@error)
Function ConvertCOMerror($error)
$error = val("&"+Right(DecToHex($error),4))
? "Error: $error"
shell "net helpmsg $error"
Endfunction

Since you are attempting to actually rename the object not a truly modifyable attribute, I do not think your approach will work. CN is part of the distinguished name that is the object. You will probably want to use the MoveHere method to move the object to the same container with a new name.
code:
;------- Renaming an object ------------------
$cont = GetObject("LDAP://@domain/OU=Sales, DC=ArcadiaBay,DC=com")
$usr = cont.MoveHere("LDAP://@domain/CN=jsmith,OU=Sales, DC=ArcadiaBay,DC=com", "CN=jjohnson")



[ 31. May 2003, 01:19: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40830 - 2003-05-31 01:53 AM Re: Changing the "common name" in AD
JohnQ Offline
Starting to like KiXtart

Registered: 2003-03-04
Posts: 171
Howard, when you right click on a name in AD users and computers and choose rename, aren't you actually renaming or "modifying" the CN? Do you feel that the MoveHere method is the best (if not the only) approach? It seems simple enough.

Thanks for your help -- you've always got the good answers. Is there a good resource that one can go to for some of these differnt methods and object properties. Just when I think I've figured something out, you come up with something new. [Smile]

Top
#40831 - 2003-05-31 02:35 AM Re: Changing the "common name" in AD
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
If you down load ADSI 2.5 you will find an adsi25.chm which is pretty good. Then there is always http://msdn.microsoft.com.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#40832 - 2003-05-31 02:41 AM Re: Changing the "common name" in AD
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
As far as object properties go, use my enumobjprops.kix script to enumerate all the properties and then go look them up on MSDN.


Function EnumObjProps($object)
Dim $Class, $Schema, $Value, $property, $cont

$Class = GetObject($object.schema)
? "Class: " + $Class.Name
? "GUID: " + $Class.GUID
? "Implemented by: " + $Class.CLSID

If $Class.Container
? ? "Container Object"
? "Class Contains:"
For Each $cont In $Class.Containment
? " " + $cont
Next
Else
? ? "Leaf Object"
EndIf

? "Mandatory Properties in this Class: "
For Each $property In $Class.MandatoryProperties
? " " + $property
Next

? ? "Optional Properties in this Class: "
For Each $property In $Class.OptionalProperties
? " " + $property
Next
EndFunction
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Chris S., Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
2 registered (Allen, ShaneEP) and 158 anonymous users online.
Newest Members
Sjaak, Biybucket, Markus1961, Ian231, kixnewbie12
17480 Registered Users

Generated in 0.356 seconds in which 0.304 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