PowerShell Script for Mailbox Provisioning

Yesterday I posted a blogcast on PowerShell-based mailbox provisioning.

Below is the script which PowerGUI executes when you click that Create from CSV file action.

What I am doing in the script is basically:

1. Prompt for the file name (param).

2. Read into into an array.

3. Check whether columns with mandatory parameters are in the file.

4. And then just go column by column constructing the new-mailbox command for each row.

5. If Password column is not present I use UPN instead (you can change to your own rule).

6. Finally, I just call Invoke-Expression for all these generated commands.

Of course in PowerGUI all you need to do is just click a button, but for all PowerShell geeks out there here’s the code:

param($file)

[array]$csv = import-csv $file

# This script implies that columns "UserPrincipalName", "name",

# "database","OrganizationalUnit" exist in the csv file

if (($csv[0].UserPrincipalName -eq $null) -or ($csv[0].Name -eq $null) -or ($csv[0].Database -eq $null) -or ($csv[0].OrganizationalUnit -eq $null) ) {throw "Parameter missing... Make sure the CSV file has the following columns: UserPrincipalName, Name, Database, OrganizationalUnit."}

# Create collection of the commands that we will invoke in the end
[collections.arraylist]$Commands=new-object system.collections.arraylist

for($i=0; $i -lt $csv.Count; $i++)
{

[void]$Commands.Add("new-mailbox -UserPrincipalName `"$($csv[$i].UserPrincipalName)`" -Name `"$($csv[$i].Name)`" -Database `"$($csv[$i].Database)`" -OrganizationalUnit `"$($csv[$i].OrganizationalUnit)`"" + ' -Password $pwd ')
}

# Add other parameters if present in the CSV

if ($csv[0].Alias -ne $null) {
for($i=0; $i -lt $csv.Count; $i++)
{
  $Commands[$i] = $Commands[$i] + " -Alias `"$($csv[$i].Alias)`""
}
}

if ($csv[0].DisplayName -ne $null) {
for($i=0; $i -lt $csv.Count; $i++)
{
$Commands[$i] = $Commands[$i] + " -DisplayName `"$($csv[$i].DisplayName)`""
}
}


if ($csv[0].FirstName -ne $null) {
for($i=0; $i -lt $csv.Count; $i++)
{
$Commands[$i] = $Commands[$i] + " -FirstName `"$($csv[$i].FirstName)`""
}
}


if ($csv[0].LastName -ne $null) {
for($i=0; $i -lt $csv.Count; $i++)
{
$Commands[$i] = $Commands[$i] + " -LastName `"$($csv[$i].LastName)`""
}
}


if ($csv[0].Initials -ne $null) {
for($i=0; $i -lt $csv.Count; $i++)
{
$Commands[$i] = $Commands[$i] + " -Initials `"$($csv[$i].Initials)`""
}
}


if ($csv[0].SamAccountName -ne $null) {
for($i=0; $i -lt $csv.Count; $i++)
{
$Commands[$i] = $Commands[$i] + " -SamAccountName `"$($csv[$i].SamAccountName)`""
}
}


for($i=0; $i -lt $csv.Count; $i++)
{
$pwd = new-object Security.SecureString

if ($csv[0].Password -ne $null) {
$csv[$i].Password.ToCharArray() | foreach { $pwd.AppendChar($_) }
} else {
$csv[$i].UserPrincipalName.ToCharArray() | foreach { $pwd.AppendChar($_)
}

}

Invoke-Expression $Commands[$i]

Tags: , , , , ,

2 Responses to “PowerShell Script for Mailbox Provisioning”


  1. 1 Jammin December 5, 2007 at 2:42 am

    Is is possible to substitute a column, say “CustomAttribute1” instead of “Alias”. It doesn’t appear that it is a simply replace of the words…I get an error stating that a “A parameter cannot be found that matches parameter name ‘CustomAttribute1’.”

  2. 2 Dmitry Sotnikov February 9, 2009 at 12:44 pm

    Jammin,

    New-Mailbox unfortunately does not have -CustomAttribute1 parameter.

    You can use set-mailbox instead once the mailboxes get created.

    See this for help information: http://technet.microsoft.com/en-us/library/bb123981.aspx

    So for example, if you have a CSV like:
    Name, CustomAttribute1
    “Dmitry Sotnikov”, Medium

    You could do something like:
    Import.csv c:\mycsv.csv | foreach-object { Set-Mailbox $_.Name -CustomAttribute1 $_.CustomAttribute1 }

    In the future, please use the discussion forums at http://PowerGUI.org to ask such questions. Lots of smart people over there.🙂

    Dmitry


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

April 2007
M T W T F S S
« Mar   May »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

%d bloggers like this: