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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and compare strings with vbscript

Status
Not open for further replies.

7Star

Technical User
Nov 17, 2005
13
SE
Hi,

I have made a script that add user accounts from a comma separated file (.csv) into Active Directory. My CSV file looks like this (shortened):

Elin,Adolfsson
Doris,Ahlander
Daniel,Akermark
Christian,Andersson
Daniel,Alkerstedt
Daniel,Alm
Claes,Andersson
Christian,Andersson

From this file it creates the account name with a prefix (00). After the prefix it adds from the first two characters in the last name and from the first two characters in the first name. So for the user Elin Adolfsson, the user name that's being created is "00adel".

The problems is when there's more than one user that have the same name like Christian Andersson in my file. And with the users Daniel Alkerstedt and Daniel Alm the same problem arise because they got the same two first characters in the first name and the last name.

So my question is how do I go from here?
My script I have looks like this:

Option Explicit
Dim oFSO, oTF, oOU, oUser, oGroup, oRoot, oFolder, oShell
Dim sCN, sOU1
Dim aLine, sLine, sLogon, sPass, sGroup, sRoot, sDN
Dim sFname, sLname, sDescription, prefix

prefix = "00"

Const InpFile = "anvandare.csv"

Const ForReading = 1
Const ForWriting = 2
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTF = oFSO.OpenTextFile(InpFile,ForReading,True)
Set oShell = CreateObject("WScript.Shell")

Do While oTF.AtEndOfStream <> True
sLine = oTF.ReadLine
aLine = split(sline, ",",-1,1)
sFname = aLine(0)
sLname = aLine(1)
'Sets container name to Last Name, First Name
sCN = sLname &"\, "&sFname
sPass = "syp9393"
sGroup = "group3"
sOU1 = "test"
sLogon = prefix & left(aLine(1),2) & left(aLine(0),2)
CreateaUser '// Call the Create User routine
Loop

Set oTF = Nothing
Set oFSO = Nothing
Set oUser = Nothing
Set oGroup = Nothing
Set oOU = Nothing
Set oRoot = Nothing

'//--------------------------
'// Create User subroutine
'//--------------------------

Sub CreateaUser()

'Bind to the domain Root
Set oRoot = GetObject("LDAP://rootDSE")
sRoot = oRoot.Get("defaultNamingContext")

'Bind to the OU where users are to be added
Set oOU = GetObject("LDAP://ou=" & sOU1 & "," & sRoot)

Set oUser = oOU.Create("user", "cn=" & sCN)

'On error resume next

'// Load fields in AD record
oUser.put "sAMAccountName", lcase(sLogon)
oUser.put "givenName", sFname
oUser.put "sn", sLname
oUser.put "DisplayName", sFname &", "&sLname
oUser.SetInfo

'// Set initial password
oUser.setpassword sPass
oUser.pwdLastSet = 0
oUser.AccountDisabled = False
oUser.SetInfo

End Sub

// Ola Magnusson
 
Samaccountname must be unique throughout the domain. If you assign userprincipalname as well, you have even to ensure uniqueness throughout the forest. You have therefore to prepare a search for a list samaccountname (usually a recordset returned from a search script). I would think the 00 prefix would be incremental according to the number of collision.
 
And sure shortcut can be conceived to make a quick check like bind to the user with the candidate samaccountname using WinNT: provider. If it fails, then you can be sure the samaccountname candidate is permissible.
 
I'm not sure if I understand the thing with WinNT:provider.
I don't have anything more in my csv file, just first name and last name in it. So from that list I want in some way create unique samaccountnames. Can't I compare the names in the file and if there's more than one name thats alike it adds a number or something to the account name?

 
strUserNameIWantotTryandUser = "00rmoy"
Set objUser = Get("WinNT://" & domainname & "/" & strUserNameIWantotTryandUser & ",user")
If Err.Number = 0 Then
'oh dear this one already exits!!! increment 01???
End If

i hate rlying on this type of thing to get info but..the recordset method is the nicest but then unless you have checking on your source data you wouldnt need to get a ref to the recordset after each add? or keep the recordset frmo when you start but also build a list of ones you ahve added in memory as you went along and then check on both...
anyhow
 
infact you might just be able to blindly try and add the account

Set oUser = oOU.Create("user", "cn=" & sCN)
If Err.NUmber <> 0 Then
'this one is already inuse increment
End If

then of course you have to mind out that the account hasnt already been setup legit for this particular user, so you will want additional logic...say somethign like Personel number??? to be sure...something has to be unique
 
I must be very stupid, or just very bad at creating scripts because I don't get it to work. Get error message that the object already exist when running it as it did before.

I added the lines in my script but I'm not sure if I have understood it right. The lines I have added is in bold text. Just wanted at this point to test to get different user names here. As you mentioned to get logical unique names some other information about each user is necessary.

Add_users.vbs

Option Explicit
Dim oFSO, oTF, oOU, oUser, oGroup, oRoot, oFolder, oShell
Dim sCN, sOU1
Dim aLine, sLine, sLogon, sPass, sGroup, sRoot, sDN
Dim sFname, sLname, sDescription, prefix, prefix2, sLogon2

prefix = "00"
prefix2 = "11"
Const InpFile = "anvandare.csv"

Const ForReading = 1
Const ForWriting = 2
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTF = oFSO.OpenTextFile(InpFile,ForReading,True)
Set oShell = CreateObject("WScript.Shell")

Do While oTF.AtEndOfStream <> True
sLine = oTF.ReadLine
aLine = split(sline, ",",-1,1)
sFname = aLine(0)
sLname = aLine(1)
'Sets container name to Last Name, First Name
sCN = sLname &"\, "&sFname
sPass = "syp9393"
sGroup = "group3"
sOU1 = "test"
sLogon = prefix & left(aLine(1),2) & left(aLine(0),2)
sLogon2 = prefix2 & left(aLine(1),2) & left(aLine(0),2)
CreateaUser '// Call the Create User routine
Loop

Set oTF = Nothing
Set oFSO = Nothing
Set oUser = Nothing
Set oGroup = Nothing
Set oOU = Nothing
Set oRoot = Nothing

'//--------------------------
'// Create User subroutine
'//--------------------------

Sub CreateaUser()

'Bind to the domain Root
Set oRoot = GetObject("LDAP://rootDSE")
sRoot = oRoot.Get("defaultNamingContext")

'Bind to the OU where users are to be added
Set oOU = GetObject("LDAP://ou=" & sOU1 & "," & sRoot)


'On error resume next

Set oUser = oOU.Create("user", "cn=" & sCN)
If Err.NUmber <> 0 Then
oUser.put "sAMAccountName", lcase(sLogon2)
oUser.SetInfo
End If


'// Load fields in AD record
oUser.put "sAMAccountName", lcase(sLogon)
oUser.put "givenName", sFname
oUser.put "sn", sLname
oUser.put "DisplayName", sFname &", "&sLname
oUser.put "name", sCN
oUser.SetInfo

'// Set initial password
oUser.setpassword sPass
oUser.pwdLastSet = 0
oUser.AccountDisabled = False
oUser.SetInfo

End Sub
 
you havent changed your sCN

this is where you are getting the duplication problem with the CreateUser method.

deosnt sCN need to be unique as well ?

Set oUser = oOU.Create("user", "cn=" & "00" & sCN)
If Err.NUmber <> 0 Then
Set oUsern = oOU.Create("user", "cn=" & "11" & sCN)

oUser.put "sAMAccountName", lcase(sLogon2)
oUser.SetInfo
End If

i know i have messed up what you prob want for CN but i think i am on the right lines???

but eventually you might need something like...?

For i = 0 to 9
Set oUser = oOU.Create("user", "cn=" & i & sCN)
If Err.Number = 0 Then
oUser.put "sAMAccountName", lcase(i & strSomething)
oUser.SetInfo
Set oUser = Nothing
Else
'continue looping
End If

Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top