Powershell Exchange script returning inconsistent results - PS weirdness

Posted by Ian on Server Fault See other posts from Server Fault or by Ian
Published on 2012-04-10T14:53:58Z Indexed on 2012/04/12 17:32 UTC
Read the original article Hit count: 333

Filed under:
|

Maybe someone can shed some light on a bit of Powershell weirdness I've come across and can't explain.

This PS script returns a list of exchange databases, their sizes and the number of mailboxes in each one:

Get-MailboxDatabase | Select Server, StorageGroupName, Name, @{Name="Size (GB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity); $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1048576KB; [math]::round($size, 2)}}, @{Name="Size (MB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity); $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1024KB; [math]::round($size, 2)}}, @{Name="No. Of Mbx";expression={(Get-Mailbox -Database $_.Identity | Measure-Object).Count}} | Format-table –AutoSize

If I add a simple 'sort name' before the 'format-table' my resulting table contains blanks where the database sizes and number of mailboxes should appear (not zeros, blank empty space).... but only in some rows, not all rows. Some rows contain numbers!

If I put the '|sort name| ' after the initial 'get-mailboxdatabase' it works fine.

Whats even weirder is if I do the following:

  1. Execute the above command
  2. Add the sort before format-table
  3. Execute the new command
  4. Execute the initial command again

What I see is different amounts in each of the three cases - all of which are incorrect. Yet 1 and 3 are the same command and the only difference with 2 is a sort. 1 and 3 should, at a minimum, return the same results. I get blanks where I should have MBs

If I add the sort after the get-mailboxdatabase, it always returns identical results (as it should).

Can anyone suggest an explanation as to what may be going on?

If its of any help in reading the expression, I've reformatted it here to make it a bit more readable:

Get-MailboxDatabase | Select Server, StorageGroupName, Name, 
    @{Name="Size (GB)";Expression={
        $objitem = (Get-MailboxDatabase $_.Identity); $path = "`\`\" 
        + $objitem.server + "`\" 
        + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"
        + $objItem.EdbFilePath.PathName.Remove(0,2); 
    $size = ((Get-ChildItem $path).length)/1048576KB; [math]::round($size, 2)
    }}, 

    @{Name="Size (MB)";Expression={
        $objitem = (Get-MailboxDatabase $_.Identity); 
        $path = "`\`\" 
        + $objitem.server + "`\" 
        + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"
        + $objItem.EdbFilePath.PathName.Remove(0,2); 
    $size = ((Get-ChildItem $path).length)/1024KB; [math]::round($size, 2)
    }}, 

    @{Name="No. Of Mbx";expression={
        (Get-Mailbox -Database $_.Identity | Measure-Object).Count
    }} 
| Format-table –AutoSize

© Server Fault or respective owner

Related posts about exchange-2010

Related posts about powershell