#184312 - 2007-12-28 05:55 PM
LDAP account creation problem
|
craigs9
Fresh Scripter
Registered: 2006-07-05
Posts: 22
Loc: London, UK
|
I have a .csv which contains around 1000 names for user account creation.
Column A is username Column B is first name Column C is Surname.
I have been trying to use the following UDF with this .csv to create the accounts but with no luck.
[code]
CreateUserAccount("domain",
"cn=users",
"cr.test",
"Cr",
"R",
"Test",)
Function CreateUserAccount($Domain,
$ContainerDN,
$Account,
optional $FirstName,
optional $MiddleInitial,
optional $LastName,
optional $Description,
optional $Office,
optional $Telephone,
optional $Email,
optional $WebPage,
optional $userPrincipalName )
Dim $aDom, $part, $sDNdom, $LDAPpath, $oContainer, $oUser
if VarTypeName($userPrincipalName) = "Empty"
$userPrincipalName = $Account + "@@" + $Domain
endif
$aDom = split($Domain,".")
for each $part in $aDom
$sDNdom = "" + $sDNdom + ",dc=" + $part
next
$LDAPpath = "LDAP://" + $Domain + "/" + $ContainerDN + $sDNdom
;? $LDAPpath
$oContainer = GetObject($LDAPpath)
if @error
? "GetObject Error: " + @error + " " + @Serror
endif
$oUser = $oContainer.Create("User", "cn=" + $Account)
if @error
? "Create Error: " + @error + " " + @Serror
endif
$oUser.Put("sAMAccountName", $Account)
$oUser.Put("givenName", $FirstName) ;First Name
$oUser.Put("Initials", $MiddleInitial) ;Initials
$oUser.Put("sn", $LastName) ;Last Name(Surname)
$oUser.Put("displayName",$LastName + iif($LastName, ", ","") +
$FirstName + iif($MiddleInitial, " ", "") +
$MiddleInitial) ;Display name
$oUser.Put("description", $Description) ;Description
$oUser.Put("physicalDeliveryOfficeName",$Office) ;Office
$oUser.Put("telephoneNumber",$Telephone);Telephone
$oUser.Put("mail",$Email) ;E-mail
$oUser.Put("wWWHomePage",$WebPage) ;Web page
$oUser.Put("userPrincipalName", $userPrincipalName) ;userPrincipalName
$oUser.SetInfo
$CreateUserAccount = @error
if $CreateUserAccount
? "SetInfo Error: " + @error + " " + @Serror
endif
Endfunction
[/code]
Whilst I just about understand how to create and use a simply array I dont understand how to create an array within an array. i.e. how to script it so that it always looks for the first name in column B and the surname in column c. I have been trying to use a bit of the above udf and a bit of readexcel2.udf but have only achieved confusion!
Can someone shed some light on this for me please?
Thanks
|
|
Top
|
|
|
|
#184319 - 2007-12-28 10:58 PM
Re: LDAP account creation problem
[Re: NTDOC]
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11634
Loc: Space
|
I have a .csv which contains around 1000 names for user account creation. Column A is username Column B is first name Column C is Surname.
I have been trying to use the following UDF with this .csv to create the accounts but with no luck.
CreateUserAccount("domain",
"cn=users",
"cr.test",
"Cr",
"R",
"Test",)
Function CreateUserAccount($Domain,
$ContainerDN,
$Account,
optional $FirstName,
optional $MiddleInitial,
optional $LastName,
optional $Description,
optional $Office,
optional $Telephone,
optional $Email,
optional $WebPage,
optional $userPrincipalName )
Dim $aDom, $part, $sDNdom, $LDAPpath, $oContainer, $oUser
if VarTypeName($userPrincipalName) = "Empty"
$userPrincipalName = $Account + "@@" + $Domain
endif
$aDom = split($Domain,".")
for each $part in $aDom
$sDNdom = "" + $sDNdom + ",dc=" + $part
next
$LDAPpath = "LDAP://" + $Domain + "/" + $ContainerDN + $sDNdom
;? $LDAPpath
$oContainer = GetObject($LDAPpath)
if @error
? "GetObject Error: " + @error + " " + @Serror
endif
$oUser = $oContainer.Create("User", "cn=" + $Account)
if @error
? "Create Error: " + @error + " " + @Serror
endif
$oUser.Put("sAMAccountName", $Account)
$oUser.Put("givenName", $FirstName) ;First Name
$oUser.Put("Initials", $MiddleInitial) ;Initials
$oUser.Put("sn", $LastName) ;Last Name(Surname)
$oUser.Put("displayName",$LastName + iif($LastName, ", ","") +
$FirstName + iif($MiddleInitial, " ", "") +
$MiddleInitial) ;Display name
$oUser.Put("description", $Description) ;Description
$oUser.Put("physicalDeliveryOfficeName",$Office) ;Office
$oUser.Put("telephoneNumber",$Telephone);Telephone
$oUser.Put("mail",$Email) ;E-mail
$oUser.Put("wWWHomePage",$WebPage) ;Web page
$oUser.Put("userPrincipalName", $userPrincipalName) ;userPrincipalName
$oUser.SetInfo
$CreateUserAccount = @error
if $CreateUserAccount
? "SetInfo Error: " + @error + " " + @Serror
endif
Endfunction
Whilst I just about understand how to create and use a simply array I dont understand how to create an array within an array. i.e. how to script it so that it always looks for the first name in column B and the surname in column c. I have been trying to use a bit of the above udf and a bit of readexcel2.udf but have only achieved confusion!
Can someone shed some light on this for me please? Thanks
|
|
Top
|
|
|
|
#184341 - 2007-12-31 12:29 PM
Re: LDAP account creation problem
[Re: NTDOC]
|
craigs9
Fresh Scripter
Registered: 2006-07-05
Posts: 22
Loc: London, UK
|
Apologies, the preview did do this so I tried to clear up the code formatting problems, when I posted it I thought it looked fine but....
So any help then guys?
|
|
Top
|
|
|
|
#184342 - 2007-12-31 01:45 PM
Re: LDAP account creation problem
[Re: craigs9]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
You are asking about arrays of arrays, but I don't even see a simple array in your script example!
A CSV file is a simple text file with each line representing a record, consisting of fields. Assuming that the fields in your data file match the sequence of args in your UDF, you can try the following example. You'll need to download my CSV() UDF and include it in your script.
Note the changes to your UDF include passing an array, using the array values instead of individual fields, Proper output (return 1 on success/0 on failure & proper exit status), and elimination of any output from the UDF.
This is NOT tested, and you should dim the vars in the loop and such.
Glenn
If Open(3,'userdata.csv')
@SERROR ' - while opening userdata.csv')
Else
$aRecord = CSV(ReadLine(3)) ; read the CSV record, convert to array
While Not @ERROR
If CreateUserAccount($aRecord)
'Failed to create user account' ?
@SERROR ?
EndIf
$aRecord = CSV(ReadLine(3))
Loop
$ = Close(3)
EndIf
Function CreateUserAccount($aUserData)
Dim $aDom, $part, $sDNdom, $LDAPpath, $oContainer, $oUser
Dim $Domain, $ContainerDN, $Account, $FirstName, $MiddleInitial, $LastName, $Description, $Office, $Telephone, $Email, $WebPage, $userPrincipalName
$Domain = $aUserData[0]
$ContainerDN = $aUserData[1]
$Account = $aUserData[2]
$FirstName = $aUserData[3]
$MiddleInitial = $aUserData[4]
$LastName = $aUserData[5]
$Description = $aUserData[6]
$Office = $aUserData[7]
$Telephone = $aUserData[8]
$Email = $aUserData[9]
$WebPage = $aUserData[10]
$userPrincipalName= $aUserData[11]
if VarTypeName($userPrincipalName) = "Empty"
$userPrincipalName = $Account + "@@" + $Domain
endif
$aDom = split($Domain,".")
for each $part in $aDom
$sDNdom = "" + $sDNdom + ",dc=" + $part
next
$LDAPpath = "LDAP://" + $Domain + "/" + $ContainerDN + $sDNdom
;? $LDAPpath
$oContainer = GetObject($LDAPpath)
if @error
? "GetObject Error: " + @error + " " + @Serror
endif
$oUser = $oContainer.Create("User", "cn=" + $Account)
if @error
? "Create Error: " + @error + " " + @Serror
endif
$oUser.Put("sAMAccountName", $Account)
$oUser.Put("givenName", $FirstName) ;First Name
$oUser.Put("Initials", $MiddleInitial) ;Initials
$oUser.Put("sn", $LastName) ;Last Name(Surname)
$oUser.Put("displayName",$LastName + iif($LastName, ", ","") +
$FirstName + iif($MiddleInitial, " ", "") +
$MiddleInitial) ;Display name
$oUser.Put("description", $Description) ;Description
$oUser.Put("physicalDeliveryOfficeName",$Office) ;Office
$oUser.Put("telephoneNumber",$Telephone);Telephone
$oUser.Put("mail",$Email) ;E-mail
$oUser.Put("wWWHomePage",$WebPage) ;Web page
$oUser.Put("userPrincipalName", $userPrincipalName) ;userPrincipalName
$oUser.SetInfo
$CreateUserAccount = Not @ERROR
Exit @ERROR
Endfunction
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#184344 - 2007-12-31 04:23 PM
Re: LDAP account creation problem
[Re: Glenn Barnas]
|
craigs9
Fresh Scripter
Registered: 2006-07-05
Posts: 22
Loc: London, UK
|
Superb, thanks very much for your help.
I didn't include an array in the example as I just couldn't figure out how to get it to get anywhere close to working. To be honest I didn't realise it is relatively easy to create an array that looks at multiple columns. I thik I was along the right lines but was just struggling to get the right combination of guesses.
I have tested it and can create accounts successfully.
Just one more question, you mention dim'ing the vars in the loop, do you mean the $aUserData[8] vars? If so should you still dim these even though the vars they are equal to i.e.telephone in the case of $telephone=$aUserData[8} have already been dim'd?
Thanks very much again for the help and your time with this
|
|
Top
|
|
|
|
#184345 - 2007-12-31 05:15 PM
Re: LDAP account creation problem
[Re: craigs9]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
No - I meant the $aRecord var, which I didn't Dim in the core script. I had not had any coffee when I wrote this, so didn't (ok - couldn't) think about dimming vars or writing REALLY good code. It was just a heads-up to you that, depending on your coding style, should give it a closer inspection once you got the basic code working.
I'm a bit torn about the way the $aUserData array is used. The way it's presented makes it very clear what you are doing inside the code, but it isn't very efficient. I'd probably use the $aUserData[] variable directly and eliminate the "$DirectName = $aUserData[]" assignments and Dims, but add clear comments to each line so there's no guessing as to what's going on. For example:
; get rid of this
Dim $Domain, $ContainerDN, $Account, $FirstName, $MiddleInitial, $LastName, $Description, $Office, $Telephone, $Email, $WebPage, $userPrincipalName
; and all of these
$Domain = $aUserData[0]
; and change all of these:
$oUser.Put("givenName", $FirstName) ;First Name
; to this format:
$oUser.Put("givenName", $aUserData[3]) ;First Name
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|