Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: How to create a unique samaccountname in AD

HTVi
TV Discussion Newsgroups

How to create a unique samaccountname in AD
thehump <thehump.3ipvnc[ at ]DoNotSpam.com> 11/11/2008 2:45:34 PM
Does anyone have a script that will check AD for a samaccountname to make sure it is unique. If it isn't, the script should append a 01, 02, 03, etc.
Thanks in advance!! -- thehump ------------------------------------------------------------------------ thehump's Profile: http://forums.techarena.in/members/thehump.htm View this thread: http://forums.techarena.in/server-scripting/1069890.htm http://forums.techarena.in
Re: How to create a unique samaccountname in AD
"Marcin" <marcin[ at ]community.nospam> 11/11/2008 3:44:58 PM
Here you go:

Const ADS_SCOPE_SUBTREE = 2

Set oConnection = CreateObject("ADODB.Connection")
Set oCommand = CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConnection

oCommand.Properties("Page Size") = 1000
oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

sName = "UserX"

bFound = False
iCount = 1

While bFound = False
oCommand.CommandText = _
"SELECT * FROM 'LDAP://dc=yourDomainName,dc=com' WHERE
objectCategory='user' " & _
"AND samAccountName='" & sName & iCount & "'"
Set oRecordSet = oCommand.Execute

If oRecordset.RecordCount > 0 Then
iCount = iCount + 1
Else
bFound = True
sOrgName = sName & iCount
End If
Wend

WScript.Echo sName

hth
Marcin

"thehump" <thehump.3ipvnc[ at ]DoNotSpam.com> wrote in message
news:thehump.3ipvnc[ at ]DoNotSpam.com...
[Quoted Text]
>
> Does anyone have a script that will check AD for a samaccountname to
> make sure it is unique. If it isn't, the script should append a 01, 02,
> 03, etc.
> Thanks in advance!!
>
>
> --
> thehump
> ------------------------------------------------------------------------
> thehump's Profile: http://forums.techarena.in/members/thehump.htm
> View this thread: http://forums.techarena.in/server-scripting/1069890.htm
>
> http://forums.techarena.in
>


Re: How to create a unique samaccountname in AD
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 11/12/2008 4:05:02 AM

"Marcin" <marcin[ at ]community.nospam> wrote in message
news:eJGy6RBRJHA.1164[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text]
> Here you go:
>
> Const ADS_SCOPE_SUBTREE = 2
>
> Set oConnection = CreateObject("ADODB.Connection")
> Set oCommand = CreateObject("ADODB.Command")
> oConnection.Provider = "ADsDSOObject"
> oConnection.Open "Active Directory Provider"
> Set oCommand.ActiveConnection = oConnection
>
> oCommand.Properties("Page Size") = 1000
> oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
>
> sName = "UserX"
>
> bFound = False
> iCount = 1
>
> While bFound = False
> oCommand.CommandText = _
> "SELECT * FROM 'LDAP://dc=yourDomainName,dc=com' WHERE
> objectCategory='user' " & _
> "AND samAccountName='" & sName & iCount & "'"
> Set oRecordSet = oCommand.Execute
>
> If oRecordset.RecordCount > 0 Then
> iCount = iCount + 1
> Else
> bFound = True
> sOrgName = sName & iCount
> End If
> Wend
>
> WScript.Echo sName
>
> hth
> Marcin
>
> "thehump" <thehump.3ipvnc[ at ]DoNotSpam.com> wrote in message
> news:thehump.3ipvnc[ at ]DoNotSpam.com...
>>
>> Does anyone have a script that will check AD for a samaccountname to
>> make sure it is unique. If it isn't, the script should append a 01, 02,
>> 03, etc.
>> Thanks in advance!!
>>
>>
>> --
>> thehump
>> ------------------------------------------------------------------------
>> thehump's Profile: http://forums.techarena.in/members/thehump.htm
>> View this thread: http://forums.techarena.in/server-scripting/1069890.htm
>>
>> http://forums.techarena.in
>>
>
>

Since sAMAccountName must be unique in the domain, no matter which class of
object, it would make sense to not restrict the query to user objects. I
would suggest:

"SELECT * FROM 'LDAP://dc=yourDomainName,dc=com' WHERE samAccountName='" &
sName & iCount & "'"


--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


Re: How to create a unique samaccountname in AD
thehump <thehump.3irnjb[ at ]DoNotSpam.com> 11/12/2008 1:55:19 PM
Thanks for the help, but i have a few questions. Shouldn't it be WScript.Echo sOrgName? Also, i don't want the return value to have a number at the end if it isn't a duplicate. How can that be done? -- thehump ------------------------------------------------------------------------ thehump's Profile: http://forums.techarena.in/members/thehump.htm View this thread: http://forums.techarena.in/server-scripting/1069890.htm http://forums.techarena.in
Re: How to create a unique samaccountname in AD
"Marcin" <marcin[ at ]community.nospam> 11/13/2008 1:10:30 AM
OK - here is the version 2 - hopefully thats' what you're looking for (to
test for a paricular user, simply its sAMAccountname at the command prompt
as the parameter of the script:
Btw. while Richard's remark is sound, using objectCategory as the query
filter has its purpose (performance benefits) - but if you suspect you might
be running into name clashes with other object types, feel free to exclude
it...

Const ADS_SCOPE_SUBTREE = 2

Set oConnection = CreateObject("ADODB.Connection")
Set oCommand = CreateObject("ADODB.Command")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
Set oCommand.ActiveConnection = oConnection

oCommand.Properties("Page Size") = 1000
oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

sName1 = WScript.Arguments(0)
sName2 = sName1

bFound = False
iCount = 0

While bFound = False
oCommand.CommandText = _
"SELECT * FROM 'LDAP://dc=yourDomainName,dc=local' WHERE
objectCategory='user'" &_
" AND sAMAccountName='" & sName2 & "'"
Set oRecordSet = oCommand.Execute

If oRecordset.RecordCount > 0 Then
iCount = iCount + 1
sName2 = Left(sName2, Len(sName1)) & iCount
Else
bFound = True
WScript.Echo sName2
End If
Wend

hth
Marcin

"thehump" <thehump.3irnjb[ at ]DoNotSpam.com> wrote in message
news:thehump.3irnjb[ at ]DoNotSpam.com...
[Quoted Text]
>
> Thanks for the help, but i have a few questions. Shouldn't it be
> WScript.Echo sOrgName? Also, i don't want the return value to have a
> number at the end if it isn't a duplicate. How can that be done?
>
>
> --
> thehump
> ------------------------------------------------------------------------
> thehump's Profile: http://forums.techarena.in/members/thehump.htm
> View this thread: http://forums.techarena.in/server-scripting/1069890.htm
>
> http://forums.techarena.in
>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen