Contact

Subscribe via Email

Subscribe via RSS

Categories

Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan

Sorting mailbox folders by size over a remote PS session

So here’s the problem: I am connected to my Exchange 2010 server via a remote PS session. I run the Get-MailboxFolderStatistics cmdlet to get a list of the folders and their sizes for a mailbox, piping it into Format-Table to get a tabular view.

By default this is sorted by the folder name. I want to sort it by folder size instead, so I pipe it to the Sort-Object cmdlet.

Not good, it is sorting by the folder size but seems to be considering it as a string and totally ignoring whether the figure is in KB or MB.

This seems to be because I am doing a remote PS session. Notice the results of Get-Member below:

As suspected, the type of FolderSize is string. And this is because the object is being serialized-deserialized (i.e. converted to XML) when being passed from the remote PS session to my local one. If I were to run this cmdlet within EMS on the server, Sort-Object will work as expected, but since I am running remotely the deserialization is messing things up.

Gotta work around this.

Via a blog post on sorting I discover the Sort-Object can take expressions to define the property used for sorting. The expression can be a string – selecting the property based on which sorting happens, and whether it’s sorted ascending or descending – or it can be an expression. The latter is what we are interested in.

Try the following:

Let’s break this down to see what I am doing here. The key bit is the code within the Expression block, so let’s focus on that:

What I do is (1) define a temp variable that takes the FolderSizeproperty and extracts the number using a regular expression. This number is actually stored as a string, mind you, considering it has commas and all, so (2) I remove the commas and convert the number to an integer and (3) output this number – which is what gets used by Sort-Object as the property to sort on. (If I skip outputting the number Sort-Object ignores the Expression block).

It’s probably not a good idea to use the [int] data type for folder size as users could have large folders and easily exceed the limit. So best to use an unsigned 64-bit integer. (Thanks to this post on the info about unsigned integers and its limits).

Putting it all together, I can finally sort the folders by size:

Yaay!

Managing Exchange 2010 from Windows XP

Although the Exchange Management Console (EMC) and Exchange Management Shell (EMS) are 64-bit tools that can only be installed on Vista SP2 or higher, it is possible to manage Exchange 2010 from a 32-bit Windows XP machine. Exchange 2010 relies on Windows PowerShell for its management tasks and the EMC and EMS are only different ways of running these PowerShell commands. Both these tools do remote PowerShell into an IIS PowerShell virtual directory published on the Exchange 2010 server and so one can install PowerShell 2.0 on Windows XP and do the same. There is one catch but that doesn’t hamper the usage much.

Here’s what you go to do:

First, download and install the Windows Management Framework for Windows XP from here. This installs PowerShell 2.0 and Windows Remote Management (WinRM) 2.0. Launch PowerShell and type the following:

Replace <exchangeserver> with the name of your Exchange 2010 server.

If you are running PowerShell as a user who is authorized to connect to the Exchange 2010 server the above command is fine. But if you need to specify credentials, then tack a -Credential switch thus:

This will prompt to enter the username/ password and then connect to Exchange 2010 remote PowerShell. You can also replace (Get-Credential)with a username and you will get prompted only for the password.

Next import the session to your existing one thus:

This could take a while. Once it completes all the usual EMS commands will work from your PowerShell window on Windows XP.

I have a snippet like this in my $PROFILE file:

What this does is that it creates the remote session, shows me the session name and an ID, but doesn’t connect to it. Whenever I want to do any Exchange related tasks I can import the session.

Now on to the one catch with this method. What happens is that because the objects are converted to XML and back when passed from server to client, some of the member types get converted into strings incorrectly. This is usually not a problem, but does make a difference with certain operations. For instance, when it comes to sizes – you can’t easily convert the size to MB or KB as the member is no longer of type Size but is type String. Similarly, when sorting, you would expect an order such as 1, 2, 3, 100, 200, 1000, etc but because the “numbers” are now strings the result you get will be 1, 100, 1000, 2, 200, 3, etc.

Update: I detail a workaround for this in a later post.