Powershell Exchange script returning inconsistent results - PS weirdness
- by Ian
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:
Execute the above command
Add the sort before format-table
Execute the new command
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