Wednesday, February 24, 2010

Powershell - Provisioning Users

Hello Everyone,

Time for my second blog, i was working on and off for the last few days on the powershell script, using which you can provision new users.

Script?
For what? - Create new mailbox enabled AD users
Input Method? - a CSV file

First of all thanks to "Don Jones" for writing such an eloborate blog on technet on how to setup this provisioning, you can read Don's blog from the below link.


http://technet.microsoft.com/en-us/magazine/2009.03.windowspowershell.aspx?pr=blog

For those newbies like me to powershell, POWERGUI is a cool little editor for writing powershell scripts, you can download it from the below link.

http://www.powergui.org/downloads.jspa

Now lets get to the tasks ahead.

First lets take a look at the CSV file, see the screenshot below:

Note the Header of each field, those don't look like standard AD attribute name, well we will tackle them when we actually write the powershell script.

Script will consist of 2 blocks , the work of the first block is just to read the CSV file and for each and every line of the CSV will be converted into an HASHTABLE and this hashtable will be fed in to the second script block.

Below screenshot of the complete code:



Lets take a look the script blocks closely.

Function ReadCSV {
Param([string]$fileName)

$users = Import-Csv $fileName

foreach ($user in $users){

$ht = @{ 'givenName'=$user.fn
'sn'=$user.ln

'displayName'=$user.dispname
'alias'=$user.alias

'samAccountName'=$user.alias

'userPrincipalName' = $user.upn

'database' = $user.Database
'
organizationalUnit' = $user.ou

'name' = ($user.fn + $user.sn)

}

Write-Output $ht
}
}


I guess, you all came to know the purpose of the function from the function name itself, well you are right, this function takes a csv file path as the input and then imports that csv file.
Using the command
Import-Csv $fileName.

Output of this command is stored on to the $users variable, in the next line you can see that using a for each loop, each line of the csv is fed into the $ht hashtable

For example, as you have in the screenshot of the csv file, displayNAme value of the hastable will be picked from the CSV file and stored onto the $ht hashtable, in this scenario it will be "Bhat, Shridhar"

Once all the fields mentioned here are loaded to the the $ht hashtable, then using the write-output command it is passed on to the next pipeline

Just by the name you may wonder, that the purpose of "Write-Output" is simply to write out some output, but that is not the case, it will feed each hashtable entry to the proceeding pipeline, in our case the second script block which will actually use this info and create the mailboxes.

You can read about "Write-Output" here.

Lets jump to the next script block.

Function CreateUser{
Param($userInfo)
$secureString = ConvertTo-SecureString "K1ll31ll$1234" -AsPlainText –Force New-Mailbox -Name $userInfo['name' ]`
-Alias $userInfo['alias'] -UserPrincipalName $userInfo['userPrincipalName'] `
-SamAccountName $userInfo['alias'] -Database $userInfo['Database']`
-FirstName $userInfo['givenName'] -LastName $userInfo['sn'] `
-DisplayName $userInfo['DISPLAYNAME'] `
-Password $secureString -ResetPasswordOnNextLogon $true
}

Notice this function takes in a parameter $userinfo , meaning the hashtable from the previous script block, you will understand better, when we look at the last step of this script, for now, it takes in a parameter.

Then, all it does is that , it uses the "New-Mailbox" cmdlet and then feeds in the parameter which it recieved from the hastable and then uses them as a parameter for the "New-Mailbox" cmdlet.

Notice this line -
$secureString = ConvertTo-SecureString "K1ll31ll$1234" -AsPlainText –Force
This for me not wanting to enter a password for each and every user, so i am forcing the script to use this plain text as a password.

Function CreateMailbox{ PROCESS { CreateUser $_ } }

This is just a blanket function from which we call the CreateUser function, this is basically to modularise the script.
Lets look at the final bit, which is the actual flow of the script.

ReadCSV c:\Scripts\UserList_10.csv | CreateMailbox

Notice, first we are calling the ReadCSV function and give it a csv file path as a parameter and then it is piped to the CreateMailbox function.
So when the first line of the csv file is read, it take all the user details into a hashtable and then feeds that info to the CreateMailbox function, which in turn , using the new-mailbox cmdlet creates the mailbox enabled AD user.

Output of the Script:



Actually i used mutiple names in the CSV files, which i didnt show in the first screenshot.

Below is the screenshot from "Active Directory Users and Computers" showing the user objects.
I user Windows Server 2008 R2 + Exchange 2010"



This one is from the Exchange 2010 Management Console.




Time to sleep, it is 6AM, i better sleep now :).

~F0x


No comments:

Post a Comment