Thursday, December 14, 2006

Quick Create Mailbox Powershell Form for Exchange 2007

A lot of times when your evaluating or testing code you may want to create some temporary mailboxes very quickly to test this or that. In Exchange 2007 the Exchange Command Shell’s new-mailbox commandlet gives a quick way of doing this. But because of the complexities of actually creating a mailbox eg knowing the Conical name of the OU and the name of the database you want to put it into etc actually typing all these values isn’t a fast or easy task. If your scripting this its not such a big deal because when your creating the script you can just cut and paste the values into your script and then you can run the thing a number of times and not have to worry about it. The Exchange Management Console provides wizards to create a mailbox but like all wizards they are designed to be easy to use not fast. So what I decided to do was see if I could put together a small 1 page form that you could run from the Exchange Command shell that would allow me to quickly put in the information that I wanted and then call the New-Mailbox commandlet to create the account.

Using the example from get-help new-mailbox as a template I created a form and populated it with textboxes to allow input of

UserPrincipalName,Alias,FirstName,LastName,DisplayName

To remove the requirement of knowing the canonical name of the OU and exact servername and database name I used Comboboxes for these values and then wired these to some other code and cmdlets to fill the values in the drop downs.

For the OU Name box an ADSI query of all the OU’s in the domain is used to populate the OU Name dropdown . For the Servername box the get-mailboxserver cmdlet is used to populate the list of mailbox servers. The servername combobox has one wired event so when a servername is select the get-mailboxdatabase cmdlet is run to then populate the Mailstore combobox for the selected severname. If the servername is change the Mailstore dropdown values should also change (to be honest I didn’t test this because I don’t have more the one server)

The password box is just a textbox as well with the UseSystemPasswordChar property set true to provide the normal password masking.

The rest of the codes is relatively simple I used some hashtables to mask some of the values to make them more practical to use in a Winform such as trimming the domain from the conical name. But the rest of the code just builds the form and then builds the new-mailbox command. The result of the command is written back to the commandline so any error messages will be displayed there.

The script only runs in the Exchange Command Shell because it’s using the Exchange cmdlets to do the work. I’ve put a down-loadable copy of the code here the code itself looks like.

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

Function createmailbox{
$psSecurePasswordString = new-object System.Security.SecureString
foreach($char in $pwPassWordTextBox.Text.ToCharArray())
{
$psSecurePasswordString.AppendChar($char)
}
$result = New-mailbox -UserPrincipalName $unUserNameTextBox.text -alias $emAliasTextBox.text -database $MBhash1[$msMailStoreDrop.SelectedItem.ToString()] `
-Name $dsDisplayNameTextBox.text -OrganizationalUnit $OUhash1[$ouOuNameDrop.SelectedItem.ToString()] -password $psSecurePasswordString `
-FirstName $unFirstNameTextBox.text -LastName $lnLastNameTextBox.text -DisplayName $dsDisplayNameTextBox.text
$msgbox = new-object -comobject wscript.shell
if ($result -ne $null){write-host "Mailbox Created Sucessfully"}
else{write-host "Error Creating Mailbox check command Line for Details"}

}


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

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

# Add UserName Box
$unUserNameTextBox = new-object System.Windows.Forms.TextBox
$unUserNameTextBox.Location = new-object System.Drawing.Size(100,30)
$unUserNameTextBox.size = new-object System.Drawing.Size(130,20)
$form.Controls.Add($unUserNameTextBox)

# Add Username Lable
$unUserNamelableBox = new-object System.Windows.Forms.Label
$unUserNamelableBox.Location = new-object System.Drawing.Size(10,30)
$unUserNamelableBox.size = new-object System.Drawing.Size(100,20)
$unUserNamelableBox.Text = "Username UPN"
$form.Controls.Add($unUserNamelableBox)

# Add Alias Box
$emAliasTextBox = new-object System.Windows.Forms.TextBox
$emAliasTextBox.Location = new-object System.Drawing.Size(100,60)
$emAliasTextBox.size = new-object System.Drawing.Size(130,20)
$form.Controls.Add($emAliasTextBox)

# Add Alias Lable
$emAliaslableBox = new-object System.Windows.Forms.Label
$emAliaslableBox.Location = new-object System.Drawing.Size(10,60)
$emAliaslableBox.size = new-object System.Drawing.Size(100,20)
$emAliaslableBox.Text = "Alias"
$form.Controls.Add($emAliaslableBox)

# Add FirstName Box
$unFirstNameTextBox = new-object System.Windows.Forms.TextBox
$unFirstNameTextBox.Location = new-object System.Drawing.Size(100,90)
$unFirstNameTextBox.size = new-object System.Drawing.Size(130,20)
$form.Controls.Add($unFirstNameTextBox)

# Add FirstName Lable
$unFirstNamelableBox = new-object System.Windows.Forms.Label
$unFirstNamelableBox.Location = new-object System.Drawing.Size(10,90)
$unFirstNamelableBox.size = new-object System.Drawing.Size(60,20)
$unFirstNamelableBox.Text = "First Name"
$form.Controls.Add($unFirstNamelableBox)

# Add LastName Box
$lnLastNameTextBox = new-object System.Windows.Forms.TextBox
$lnLastNameTextBox.Location = new-object System.Drawing.Size(100,120)
$lnLastNameTextBox.size = new-object System.Drawing.Size(130,20)
$form.Controls.Add($lnLastNameTextBox)

# Add LastName Lable
$lnLastNamelableBox = new-object System.Windows.Forms.Label
$lnLastNamelableBox.Location = new-object System.Drawing.Size(10,120)
$lnLastNamelableBox.size = new-object System.Drawing.Size(60,20)
$lnLastNamelableBox.Text = "Last Name"
$form.Controls.Add($lnLastNamelableBox)

# Add DisplayName Box
$dsDisplayNameTextBox = new-object System.Windows.Forms.TextBox
$dsDisplayNameTextBox.Location = new-object System.Drawing.Size(100,150)
$dsDisplayNameTextBox.size = new-object System.Drawing.Size(130,20)
$form.Controls.Add($dsDisplayNameTextBox)

# Add DisplayName Lable
$dsDisplayNamelableBox = new-object System.Windows.Forms.Label
$dsDisplayNamelableBox.Location = new-object System.Drawing.Size(10,150)
$dsDisplayNamelableBox.size = new-object System.Drawing.Size(100,20)
$dsDisplayNamelableBox.Text = "Display Name"
$form.Controls.Add($dsDisplayNamelableBox)

# Add OU Drop Down
$ouOuNameDrop = new-object System.Windows.Forms.ComboBox
$ouOuNameDrop.Location = new-object System.Drawing.Size(100,180)
$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,180)
$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,210)
$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,210)
$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,240)
$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,240)
$msMailStorelableBox.size = new-object System.Drawing.Size(100,20)
$msMailStorelableBox.Text = "Mail-Store"
$form.Controls.Add($msMailStorelableBox)

# Add Password Box
$pwPassWordTextBox = new-object System.Windows.Forms.TextBox
$pwPassWordTextBox.Location = new-object System.Drawing.Size(100,270)
$pwPassWordTextBox.size = new-object System.Drawing.Size(130,20)
$pwPasswordTextBox.UseSystemPasswordChar = $true
$form.Controls.Add($pwPassWordTextBox)

# Add Password Lable
$pwPassWordlableBox = new-object System.Windows.Forms.Label
$pwPassWordlableBox.Location = new-object System.Drawing.Size(10,270)
$pwPassWordlableBox.size = new-object System.Drawing.Size(60,20)
$pwPassWordlableBox.Text = "Password"
$form.Controls.Add($pwPassWordlableBox)

# Add Create Button

$crButton = new-object System.Windows.Forms.Button
$crButton.Location = new-object System.Drawing.Size(110,310)
$crButton.Size = new-object System.Drawing.Size(100,23)
$crButton.Text = "Create Mailbox"
$crButton.Add_Click({CreateMailbox})
$form.Controls.Add($crButton)

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

10 comments:

Prakash said...

This sounds great / Can such concept used to create WINFORMS for all Exchange CMDLETS thus allowing an Exchange admin to not have to use Shell at all and exposing all functions in CMDLETS to a GUI ??..

Glen said...

Sure its just a little time consuming but in theory you could write you Exchange admin program with the functions you want your admins to run. All this stuff can be invoked remotely so its a simple way of getting an Exchange admin program out to other amdins that easy to use and you can run anywhere theres powershell cmd shell. (or compile it as an Exe in C#)

Cheers
Glen

Anonymous said...

I get an error when I try to run this script from my Exchange server stating the script is not digitally signed. I am not very good with scripting so be easy on me.

Thanks,

Glen said...

To fix the signing issue all you need to do is in Windows Explorer right click the file and select properties. Then from the General Tab click Unblock. This will allow you to run the script without needing it to be signed.

Cheers
Glen

fx said...
This comment has been removed by the author.
Anonymous said...

hey.. this works great for the root. how can I get it to search a sub domain for OU's to put users in?

Glen said...

You need to first get a list of all the domains in the forest then create code that executed the OU Drop drown (remarked with # Add OU Drop Down) routine for each of the domain returned.

Cheers
Glen

Anonymous said...

Hi,

How can i modify the script so that the UPN is set by the FirstName.LastName textboxes and a suffix.

Thankyou

Anonymous said...

For sme reason this does not work in exchange 2010. The database input is not accepted.

Cannot process argument transformation on parameter 'Database'. Cannot convert value

If you leave the server out it works but then you would need to run it on the db server.

Christopher said...

@LillaBear

To make it work in EX2010 there are a couple updates that will need to be made. You will need to update the MBHash1 section a bit, as well as you will need to include the following on line 3:

Add-PSSnapin *exchange*

That should fix most of the issues that will arise with the script and 2010.