Monday, July 05, 2010

Multi-User quick create Powershell form for Exchange 2007

One of the more mind numbing tasks that Exchange administrators perform is creating new users and mailboxes. If you've ever been asked to create 5-10 users at the same time and are staring at a number of very repedative mouse clicks especially if you detests wizards then this script can help out. This is version 2 of this script i created a number of years ago that allowed creating just one user at at time this improves on this original idea by using a Datagrid instead so allowing you to enter multiple users and being able to create the users with just one click (well hopefully). Its also quite usefull in development enviroments where you might want to create a large number of users at once. The script itself is fairly simple in that it just wrappers a simple Winform around the create mailbox command and users a Datagrid for multiline input.

I've put a download of the code here the script itself looks like

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

Function createmailbox{
foreach($row in $dgDataGrid.rows){
if ($row.Cells[0].Value -ne $null){
$psSecurePasswordString = new-object System.Security.SecureString
foreach($char in $row.Cells[5].Value.ToCharArray())
{
$psSecurePasswordString.AppendChar($char)
}
$result = New-mailbox -UserPrincipalName $row.Cells[0].Value -alias $row.Cells[4].Value -database $MBhash1[$msMailStoreDrop.SelectedItem.ToString()] `
-Name $row.Cells[3].Value -OrganizationalUnit $OUhash1[$ouOuNameDrop.SelectedItem.ToString()] -password $psSecurePasswordString `
-FirstName $row.Cells[1].Value -LastName $row.Cells[2].Value -DisplayName $row.Cells[3].Value
if($result -ne $null){
[system.windows.forms.messagebox]::Show("Mailbox " + $result + " Created")
}
else{
[system.windows.forms.messagebox]::Show("Error createing " + $row.Cells[0].Value + " check cmdline")
}

}
}

}

$OUhash1 = @{ }
$MBhash1 = @{ }

$form = new-object System.Windows.Forms.form
$form.Text = "Exchange 2007 Quick User Create Form"
$form.size = new-object System.Drawing.Size(800,800)

$msTable = New-Object System.Data.DataTable

$msTable.TableName = "GroupName"
$msTable.Columns.Add("UPN-AccountName")
$msTable.Columns.Add("FirstName")
$msTable.Columns.Add("LastName")
$msTable.Columns.Add("DisplayName")
$msTable.Columns.Add("Alias")
$msTable.Columns.Add("Password")


# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGridView
$dgDataGrid.Location = new-object System.Drawing.Size(10,10)
$dgDataGrid.size = new-object System.Drawing.Size(750,200)
$dgDataGrid.AutoSizeRowsMode = "AllHeaders"
$form.Controls.Add($dgDataGrid)

$dgDataGrid.DataSource = $msTable


# Add OU Drop Down
$ouOuNameDrop = new-object System.Windows.Forms.ComboBox
$ouOuNameDrop.Location = new-object System.Drawing.Size(100,260)
$ouOuNameDrop.Size = new-object System.Drawing.Size(230,30)
$ouOuNameDrop.Items.Add("/Users")
$OUhash1.Add("/Users","Users")
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = '(objectClass=organizationalUnit)'
$searcher.PropertiesToLoad.Add("canonicalName")
$searcher.PropertiesToLoad.Add("Name")
$searcher1 = $searcher.FindAll()
foreach ($person in $searcher1){
[string]$ent = $person.Properties.canonicalname
$OUhash1.Add($ent.substring($ent.indexof("/"),$ent.length-$ent.indexof("/")),$ent)
$ouOuNameDrop.Items.Add($ent.substring($ent.indexof("/"),$ent.length-$ent.indexof("/")))
}
$form.Controls.Add($ouOuNameDrop)


# Add OU DropLable
$ouOuNamelableBox = new-object System.Windows.Forms.Label
$ouOuNamelableBox.Location = new-object System.Drawing.Size(10,260)
$ouOuNamelableBox.size = new-object System.Drawing.Size(100,20)
$ouOuNamelableBox.Text = "OU Name"
$form.Controls.Add($ouOuNamelableBox)

# Add Server Drop Down
$snServerNameDrop = new-object System.Windows.Forms.ComboBox
$snServerNameDrop.Location = new-object System.Drawing.Size(100,290)
$snServerNameDrop.Size = new-object System.Drawing.Size(130,30)
get-mailboxserver | ForEach-Object{$snServerNameDrop.Items.Add($_.Name)}
$snServerNameDrop.Add_SelectedValueChanged({
$msMailStoreDrop.Items.Clear()
get-mailboxdatabase -Server $snServerNameDrop.SelectedItem.ToString()| ForEach-Object{$msMailStoreDrop.Items.Add($_.Name)
$MBhash1.add($_.Name,$_.ServerName + "\" + $_.StorageGroup.Name + "\" + $_.Name)
}
})
$form.Controls.Add($snServerNameDrop)

# Add Server DropLable
$snServerNamelableBox = new-object System.Windows.Forms.Label
$snServerNamelableBox.Location = new-object System.Drawing.Size(10,290)
$snServerNamelableBox.size = new-object System.Drawing.Size(100,20)
$snServerNamelableBox.Text = "ServerName"
$form.Controls.Add($snServerNamelableBox)

# Add MailStore Drop Down
$msMailStoreDrop = new-object System.Windows.Forms.ComboBox
$msMailStoreDrop.Location = new-object System.Drawing.Size(100,320)
$msMailStoreDrop.Size = new-object System.Drawing.Size(130,30)
$form.Controls.Add($msMailStoreDrop)

# Add MailStore DropLable
$msMailStorelableBox = new-object System.Windows.Forms.Label
$msMailStorelableBox.Location = new-object System.Drawing.Size(10,320)
$msMailStorelableBox.size = new-object System.Drawing.Size(100,20)
$msMailStorelableBox.Text = "Mail-Store"
$form.Controls.Add($msMailStorelableBox)


# Add Create Button

$crButton = new-object System.Windows.Forms.Button
$crButton.Location = new-object System.Drawing.Size(10,360)
$crButton.Size = new-object System.Drawing.Size(150,23)
$crButton.Text = "Create Mailboxs"
$crButton.Add_Click({CreateMailbox})
$form.Controls.Add($crButton)

$form.topmost = $true
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()

2 comments:

YE MIN WIN said...

Anyway to copy and paste the filed from excel spreed sheet to data grid?
How to put the other attribute such as company field at data grid.

Thanks a lot.

Anonymous said...

Can this made to work in Exchange 2010.

Now it says it does not understand the database name as the script gives it in the form of server\database

(even thought ms documents say it sould work it does not. if i manually run new-mailbox I cannot get it to work when giving server name. Without works fine.)