Wednesday, March 10, 2010

Mailbox Size Gui - Exchange 2010 Remote Powershell version

One of the most popular scripts I've posted over the past few years has been the mailbox size gui for Exchange 2007 which evolved over a few different versions. While this is really only scratching the surface of the kind of cool things you can do with this information time and life hasn't really allowed me to make it better. But what i have managed to do is to port it to work with Exchange 2010 using remote powershell from a Windows 7 workstation.

A few challenges that needed to be solved to do this was first a front-end winform that allowed you to select the Exchange server you want to remote the powershell session to. Then some simple code to establish the remote PS Session which look like this


$form1 = new-object System.Windows.Forms.form
$form1.Text = "Exchange 2010 Remote PS Select"
$form1.size = new-object System.Drawing.Size(400,200)
$ServerName = ""
$conuri = ""
# Add Server DropLable
$snServerNamelableBox1 = new-object System.Windows.Forms.Label
$snServerNamelableBox1.Location = new-object System.Drawing.Size(10,20)
$snServerNamelableBox1.size = new-object System.Drawing.Size(120,20)
$snServerNamelableBox1.Text = "Remote ServerName"

$exButton4 = new-object System.Windows.Forms.Button
$exButton4.Location = new-object System.Drawing.Size(10,80)
$exButton4.Size = new-object System.Drawing.Size(125,20)
$exButton4.Text = "Connect"
$conuri = "http://" + $ServerName + "/PowerShell/"

# Add Server Drop Down
$snServerNameDrop1 = new-object System.Windows.Forms.ComboBox
$snServerNameDrop1.Location = new-object System.Drawing.Size(150,20)
$snServerNameDrop1.Size = new-object System.Drawing.Size(160,30)
$snServerNameDrop1.Add_SelectedValueChanged({$ServerName = $snServerNameDrop1.SelectedItem.ToString()})


$root = [ADSI]'LDAP://RootDSE'
$cfConfigRootpath = "LDAP://" + $root.ConfigurationNamingContext.tostring()
$configRoot = [ADSI]$cfConfigRootpath
$searcher = new-object System.DirectoryServices.DirectorySearcher($configRoot)
$searcher.Filter = '(objectCategory=msExchExchangeServer)'
$searchres = $searcher.FindAll()
foreach ($res in $searchres){
$srvOjb = $res.Properties

$form1.autoscroll = $true

The next challenge was to do with the way the objects are sent and recieved between remote ps session which is explained in detail here. What this menas is that the Byte Quantified Size type isn't availble so what you recieve at the remote end is the string format version of the data structure which needs a simple parse to retrieve the byte information. With this

function CovertBitValue($String){
$numItempattern = '(?=\().*(?=bytes)'
$matchedItemsNumber = [regex]::matches($String, $numItempattern)
$Mb = [INT64]$matchedItemsNumber[0].Value.Replace("(","").Replace(",","")
return [math]::round($Mb/1048576,0)

This still needs a little more testing but seems to work okay I've put a download of the new script here.