Home All Groups Group Topic Archive Search About

Creating user's CN from Last and First name



Author
6 Nov 2007 10:48 AM
Bartosz Kowalski
Hi,

Is it possible to create user with cn=LasName, FirstName ??
You can do it (by default) using AD Users management on W2K3 Server.
How can I do it by VBscript?

Author
6 Nov 2007 3:39 PM
Richard Mueller [MVP]
Bartosz Kowalski wrote:

>
> Is it possible to create user with cn=LasName, FirstName ??
> You can do it (by default) using AD Users management on W2K3 Server.
> How can I do it by VBscript?

Commas embedded in the Common Name must be escaped with the backslash escape
character. In brief:
========
strFirstName = InputBox("Enter new user first name")
strLastName = InputBox("Enter new user last name")

' Hard coded parent container.
strParent = GetObject("LDAP://ou=Sales,ou=West,dc=MyDomain,dc=com")

' Create new user object in the parent container.
' Note that the comma is escaped.
Set objUser = objParent.Create("user", "cn=" & strLastName & "\, " &
strFirst Name)

' Assign "pre-Windows 2000 logon name" to be first initial and last name.
' This value must be unique in the domain.
objUser.sAMAccountName = Left(strFirstName, 1) & strLastName

' Make user account enabled.
objUser.AccountDisabled = False

' Save new user object in AD
objUser.SetInfo

' Assign password.
objUser.SetPassword = "xyz312"

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
Author
7 Nov 2007 9:10 AM
Bartosz Kowalski
I've made the following script:

strFirstName=InputBox("Enter first name: ")
strLastName=InputBox("Enter last name: ")

lenF=Len(strFirstName)
lenL=Len(strLastName)

strCN=UCase(Left(strLastName,1)) & LCase(Mid(strLastName, 2, lenL-1)) & "\,
" & UCase(Left(strFirstName,1)) & LCase(Mid(strFirstName, 2, lenF-1))

strLogin=Left(LCase(strLastName),2) & LCase(Left(strFirstName,1))

Set objOU = GetObject("LDAP://cn=users,dc=mydomain")

Set objUser = objOU.Create("User", "cn=" & strLastName & "\, " & strFirstName)
objUser.Put "sAMAccountName", strLogin
objUser.Put "sn", strFirstName
objUser.Put "givenName", strLastName
objUser.Put "displayName", strCN
objUser.Put "Name", strCN
objUser.Put "userPrincipalName", strLogin & "@mydomain"
objUser.put "distinguishedName", strCN
objUser.SetInfo

and I received an error message "80072032 An invalid dn syntax has been
specified".

Show quote
"Richard Mueller [MVP]" wrote:

> Bartosz Kowalski wrote:
>
> >
> > Is it possible to create user with cn=LasName, FirstName ??
> > You can do it (by default) using AD Users management on W2K3 Server.
> > How can I do it by VBscript?
>
> Commas embedded in the Common Name must be escaped with the backslash escape
> character. In brief:
> ========
> strFirstName = InputBox("Enter new user first name")
> strLastName = InputBox("Enter new user last name")
>
> ' Hard coded parent container.
> strParent = GetObject("LDAP://ou=Sales,ou=West,dc=MyDomain,dc=com")
>
> ' Create new user object in the parent container.
> ' Note that the comma is escaped.
> Set objUser = objParent.Create("user", "cn=" & strLastName & "\, " &
> strFirst Name)
>
> ' Assign "pre-Windows 2000 logon name" to be first initial and last name.
> ' This value must be unique in the domain.
> objUser.sAMAccountName = Left(strFirstName, 1) & strLastName
>
> ' Make user account enabled.
> objUser.AccountDisabled = False
>
> ' Save new user object in AD
> objUser.SetInfo
>
> ' Assign password.
> objUser.SetPassword = "xyz312"
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>
Author
7 Nov 2007 1:10 PM
Richard Mueller [MVP]
You cannot assign a value to the distinguishedName or Name attributes. When
the object is created with:

"cn=" & strLastName & "\, " & strFirstName

the Distinguished Name becomes:

"cn=" & strLastName & "\, " & strFirstName & ",cn=users,dc=mydomain"

It may be that you want to use this instead:

Set objUser = objOU.Create("user", "cn=" & strCN)

You also cannot assign a value to Name. The Name attribute is the "Relative
Distinguished Name" of the object, which in your case will be:

"cn=" & strCN

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

Show quote
"Bartosz Kowalski" <BartoszKowal***@discussions.microsoft.com> wrote in
message news:328121B4-77C5-4226-848F-AF3C28C09E1B@microsoft.com...
> I've made the following script:
>
> strFirstName=InputBox("Enter first name: ")
> strLastName=InputBox("Enter last name: ")
>
> lenF=Len(strFirstName)
> lenL=Len(strLastName)
>
> strCN=UCase(Left(strLastName,1)) & LCase(Mid(strLastName, 2, lenL-1)) &
> "\,
> " & UCase(Left(strFirstName,1)) & LCase(Mid(strFirstName, 2, lenF-1))
>
> strLogin=Left(LCase(strLastName),2) & LCase(Left(strFirstName,1))
>
> Set objOU = GetObject("LDAP://cn=users,dc=mydomain")
>
> Set objUser = objOU.Create("User", "cn=" & strLastName & "\, " &
> strFirstName)
> objUser.Put "sAMAccountName", strLogin
> objUser.Put "sn", strFirstName
> objUser.Put "givenName", strLastName
> objUser.Put "displayName", strCN
> objUser.Put "Name", strCN
> objUser.Put "userPrincipalName", strLogin & "@mydomain"
> objUser.put "distinguishedName", strCN
> objUser.SetInfo
>
> and I received an error message "80072032 An invalid dn syntax has been
> specified".
>
> "Richard Mueller [MVP]" wrote:
>
>> Bartosz Kowalski wrote:
>>
>> >
>> > Is it possible to create user with cn=LasName, FirstName ??
>> > You can do it (by default) using AD Users management on W2K3 Server.
>> > How can I do it by VBscript?
>>
>> Commas embedded in the Common Name must be escaped with the backslash
>> escape
>> character. In brief:
>> ========
>> strFirstName = InputBox("Enter new user first name")
>> strLastName = InputBox("Enter new user last name")
>>
>> ' Hard coded parent container.
>> strParent = GetObject("LDAP://ou=Sales,ou=West,dc=MyDomain,dc=com")
>>
>> ' Create new user object in the parent container.
>> ' Note that the comma is escaped.
>> Set objUser = objParent.Create("user", "cn=" & strLastName & "\, " &
>> strFirst Name)
>>
>> ' Assign "pre-Windows 2000 logon name" to be first initial and last name.
>> ' This value must be unique in the domain.
>> objUser.sAMAccountName = Left(strFirstName, 1) & strLastName
>>
>> ' Make user account enabled.
>> objUser.AccountDisabled = False
>>
>> ' Save new user object in AD
>> objUser.SetInfo
>>
>> ' Assign password.
>> objUser.SetPassword = "xyz312"
>>
>> --
>> Richard Mueller
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>>
>>

AddThis Social Bookmark Button