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
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