Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching the Active Directory 1

Status
Not open for further replies.

teckiejon

Technical User
Feb 25, 2004
47
GB
I have written a script that creates new user accounts in the active directory. Part of the script creates a username from the details that have been input using the first 6 letters of the surname and the first initial of the firstname. For example Peter Richards would become richarp. I would like first to check that the username is not already being used which I have managed however there are a variety of locations where the user name could be which is where I am my script has currently ground to a halt.
I propose to use an array of locations and assume that I would need to loop through the array but have tried a couple of different ways none of which seem to work. the script that I have so far is this:

firstname=inputbox("Type the users first name")
if firstname =""then
wscript.echo "You didn't enter a recognised name"
wscript.quit
end if
lastname1=inputbox("type the users surname")
if lastname1 =""then
wscript.echo "You didn't enter a recognised surname"
wscript.quit
end if
lastname= UCase(lastname1)
initial = LCase(Left(firstName, 1))
username1 = LCase(left(lastName,6))
username = username1 & initial
bothName = firstName & " " & lastName
strdisplayname = lastname & " " & firstname & " -Domain"
strdisplayname1 = lastname & ", " & firstname & " -Domain"

Set objDomain = GetObject("LDAP://" & strldap)'this variable is the path to the container where the user will be created
For Each objUser In objDomain
If(objUser.sAMAccountName) = (username) Then
wscript.echo "Username : " & username & " is already being used in this container."
initial = LCase(Left(firstName, 2))
username1 = LCase(left(lastName,6))
username = username1 & initial
Exit For
End If
Next

wscript.echo "Your user has been given the following username: " & username

A problem has also arisen in that although the usernames that we now use are all lowercase previous usernames were a mixture of upper and lower laces letters and searches on the active directory have not yielded results even when looking in the correct container. Could anybody point me in the right direction? To deal with the array my most recent attempte is:

dim names(4)
names(0)="OU= container1"
names(1)="OU= container2"
names(2)="OU= container3"
names(3)="OU= container4"

for i= 0 to 3

Set objDomain = GetObjects("LDAP://" & names()+i)
For Each objUser In objDomain
If(objUser.sAMAccountName) = (username) Then
wscript.echo "Username : " & username & " is already being used in this container."
initial = LCase(Left(firstName, 2))
username1 = LCase(left(lastName,6))
username = username1 & initial
Exit For
End If
Next
next

But this doesn't work!
 
Apologies for wasting your time on this after correcting my typo I am now able to search recursively using this:

Set objDomain = GetObject("LDAP://" & names(i))

I still need to seargh regardless of case though and would appreciate any help.....
 
Have you tried to replace this:
Set objDomain = GetObjects("LDAP://" & names()+i)
By this ?
Set objDomain = GetObjects("LDAP://" & names(i))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The work around I have so far is to do 2 searches one for LCase and then a second for UCase the problem is that these seem to be quite CPU and time consuming but atleast they work. If anybody has any further input I'd really appreciate it. Thanks for your help PHV.

for i= 0 to 7

Set objDomain = GetObject("LDAP://" & names(i))
For Each objUser In objDomain
If(objUser.sAMAccountName) = (username) Then
wscript.echo "Username : " & username & " is already being used in this container."
initial = LCase(Left(firstName, 2))
username1 = LCase(left(lastName,6))
username = username1 & initial
Exit For
End If
Next
next

for i= 0 to 7

Set objDomain = GetObject("LDAP://" & names(i))
For Each objUser In objDomain
If(objUser.sAMAccountName) = UCase(username) Then
wscript.echo "Username : " & username & " is already being used in this container."
initial = LCase(Left(firstName, 2))
username1 = LCase(left(lastName,6))
username = username1 & initial
Exit For
End If
Next
next
wscript.echo "Your user has been given the following username: " & username
 
You may consider only one loop:
If UCase(objUser.sAMAccountName) = UCase(username) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV
Just tried your suggestion out and it works fine and finds users with both upper and lower case names. Brilliant as that has saved me the time it takes to do two lengthy seraches in AD

Thankyou
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top