Always A-HEAD, By being ahead you are always master of time

Hits

BOOKS

Monday, February 05, 2007

Bulk User Creation using PowerShell

When we think of making any changes in AD, we must start at creating objects. And as always first thing that comes to Mind is to create multiple users. And again here reference are same as previous one's MOW and Arulk. MOW's concept is used but methods I have to use from Arulk, cause things work.

Usually you get a excel sheet from the HR etc dept, I would always convert it into CSV since PowerShell will do the magic for me.

Contents of CSV file [Busers.csv]

Below are the headers of the CSV file and you can fill in the data.

CN,SN,GivenName,Name,Title,Description,PostalCode,TelephoneNumber,Department,Company,StreetAddress,Countrycode,SamAccountName,userPrincipalName,Mail,HomePhone,mobile.

I've shared the CSV file using google docs. Click Here

I'm going to create bulk users in India OU, which is under Zarays' OU

$IndiaOU=[ADSI]LDAP://localhost:389/ou=India,dc=zarays,dc=com
# Connecting to India OU

$UserDetails=Import-Csv "Buser.csv" #-----importing bulkusers data

foreach($UD in $UserDetails) { #--------looping into csv file and

# Passing all data into variables
$CN=$UD.CN
$SN=$UD.SN
$title=$UD.title
$description=$UD.description
$department=$UD.department
$streetAddress=$UD.streetAddress
$postalcode=$UD.postalcode
$telephoneNumber=$UD.telephoneNumber
$givenName=$UD.givenName
$company=$UD.company
$mail=$UD.mail
$homePhone=$UD.homePhone
$mobile=$UD.mobile
$userPrincipalName=$UD.userPrincipalName
$Samaccountname=$UD.Samaccountname

$Indiauser=$IndiaOU.create("user","cn=$cn") #I created actual user here and later I'm

#filling all properties for user
$Indiauser.Put("sAMAccountName",$Samaccountname)
$Indiauser.put("SN",$SN)
$Indiauser.put("Title",$Title)
$Indiauser.put("Description",$description)
$Indiauser.put("department",$department)
$Indiauser.put("streetAddress",$streetAddress)
$Indiauser.put('Postalcode',$postalcode)
$Indiauser.put('telephoneNumber',$telephoneNumber)
$Indiauser.put('givenName',$givenName)
$Indiauser.put('company',$company)
$Indiauser.put('mail',$mail)
$Indiauser.put('homePhone',$homePhone)
$Indiauser.put('mobile',$mobile)
$Indiauser.put('userPrincipalName',$userPrincipalName)

$Indiauser.setinfo() #All data committed. There are lots of other properties you can

#add
}

Below here I'm doing two things, First is enable the user and second set password. Because for some reason I'm not able to get these two things done in above loop. It throws exception. But I'm sure it can be included.

$IndiaOU=new-object directoryservices.directoryentry("LDAP://ou=India,dc=zarays,dc=com")
$UserDetails=Import-Csv "Buser.csv"

$userdetails=$IndiaOU.psbase.Children

foreach($UD in $UserDetails) {
$CN=$UD.CN
$accts=$IndiaOU.psbase.Children.Find("cn=$CN")
$accts.psbase.Invoke("SetPassword","P@ssW0Rd")
$accts.psbase.InvokeSet('Accountdisabled',$false)
$accts.psbase.CommitChanges()
}

Last and least, I'm sure there much better CMDlets to do this job, but what makes me proud it that I've created this script and I understand it very well. As compare to those VBScript scripts available on the net, I can use it but can't customize it. Thanks GOD there is PowerShell Team of Blogs.

Technorati tags: ,

IceRocket tags: ,

del.icio.us tags: ,

UPDATED :Please check comment where you will see the reason Why code has been updated.

Marc !! Bingo here it goes..the way it should work.

$IndiaOU=[ADSI]"LDAP://localhost:389/ou=Singapore,dc=zarays,dc=com"$UserDetails=Import-Csv "latestusers.csv"
foreach($UD in $UserDetails) {

$CN=$UD.CN

$SN=$UD.SN

$title=$UD.title

$description=$UD.description

$department=$UD.department

$streetAddress=$UD.streetAddress

$postalcode=$UD.postalcode

$telephoneNumber=$UD.telephoneNumber

$givenName=$UD.givenName

$company=$UD.company

$mail=$UD.mail

$homePhone=$UD.homePhone

$mobile=$UD.mobile

$userPrincipalName=$UD.userPrincipalName

$Samaccountname=$UD.Samaccountname

$Indiauser=$IndiaOU.create("user","cn=$cn")

$Indiauser.Put("sAMAccountName",$Samaccountname)

$Indiauser.put("SN",$SN)

$Indiauser.put("Title",$title)

$Indiauser.put("Description",$description)

$Indiauser.put("department",$department)

$Indiauser.put("streetAddress",$streetAddress)

$Indiauser.put('telephoneNumber',$telephoneNumber)

$Indiauser.put('givenName',$givenName)

$Indiauser.put('company',$company)

$Indiauser.put('mail',$mail)

$Indiauser.put('homePhone',$homePhone)

$Indiauser.put('mobile',$mobile)

$Indiauser.put('userPrincipalName',$userPrincipalName)

$Indiauser.setinfo()

$Indiauser.psbase.Invoke("SetPassword","P@ssW0Rd")

$Indiauser.psbase.InvokeSet('Accountdisabled',$false)

$Indiauser.psbase.CommitChanges()

}

2 comments:

/\/\o\/\/ said...

hhi PReetam,

You need to do a commitChanges() or setinfo() before you set the password, as the object has to exist first.

I did put some more info on the NG

Greetings /\/\o\/\/

PReetamZ said...

Well, for some reason I've to fill the data into variables because code is unable to understand for example $UD.userPrincipalName