1

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

1 Answer 1

0

I'm sort of thinking aloud here, but maybe this is related to this known issue with PowerShell 2.0 which has been resolved in PowerShell 3.0 CTP2. I know you don't explicitly call Add-Member, but the Select statement with a calculated field sort of accomplishes the same thing. It seems like they could be related. I ran some tests to compare PS 2.0 and 3.0 and 3.0CTP2 never once failed on your query. PowerShell 2.0 gave me different output every time.

If you can't install PS 3.0 yet, I've rewritten part of your script, but it doesn't have all of the columns that your original does. I'll let you tweak it to perfection. You'll just need to use Add-Member to add more properties.

Get-MailboxDatabase -IncludePre | foreach-object {add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinGB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter ('name=''' + $_.edbfilepath.pathname.replace("\","\\") + '''')).filesize / 1GB),2)) ; add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinMB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter ('name=''' + $_.edbfilepath.pathname.replace("\","\\") + '''')).filesize / 1MB),2)); add-member -inputobject $_ -membertype noteproperty -name numberofMailboxes -value (($_ | Get-Mailbox | Measure-Object).Count) -PassThru} | Sort-Object mailboxdbsizeinGB -Descending | format-table identity,mailboxdbsizeinGB,mailboxdbsizeinMB,numberofMailBoxes -Autosize

Keep in mind that if you install PS 3.0, you can still launch version 2.0 if you add the -version 2.0 parameter after the powershell.exe call.

2
  • yes, it could be that. However it executes in seconds, which would seem to indicate that its not timing out but that some of the sub-queries are failing. I was just a little perplexed as I've never seen similar behaviour and still don't know any way of working out what might be going on here... its not like its throwing exceptions or anything helpful.
    – Ian Murphy
    Apr 16, 2012 at 12:34
  • I'm seeing the same problem with your script. If I execute it as it is I get one set of results. If I change the sort-order to sort by database ('identity') the counts are different - perplexing.
    – Ian Murphy
    Apr 17, 2012 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .