Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: How to create a unique samaccountname in AD

Reply
Thread Tools Display Modes

Re: How to create a unique samaccountname in AD

 
 
Marcin
Guest
Posts: n/a

 
      11-11-2008
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" <> wrote in message
news:...
>
> 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
>



 
Reply With Quote
 
 
 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      11-12-2008


"Marcin" <> wrote in message
news:...
> 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" <> wrote in message
> news:...
>>
>> 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
--


 
Reply With Quote
 
Marcin
Guest
Posts: n/a

 
      11-13-2008
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" <> wrote in message
news:...
>
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Query NTFS file unique ID (volume unique, 16 bytes) Jason Windows Vista Drivers 0 07-14-2008 07:21 AM
script to create a file with unique name cosimo Windows Server 3 06-11-2005 02:59 PM
sAMAccountName Nick Active Directory 9 11-24-2004 07:26 AM
sAMAccountName Tom G. Active Directory 1 07-15-2004 09:15 PM
UPN vs sAMAccountName Arild Bakken Active Directory 5 05-04-2004 01:29 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59