sort-object not working

by penguinviw at 2013-04-02 11:55:33

I have this script, but it does not sort.
Import-Module ActiveDirectory
if(!(Get-PSSnapin |
Where-Object {$.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}
$ErrorActionPreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
$oulist = Get-OrganizationalUnit "KT" -SingleNodeOnly
ForEach ($Ounames in $Oulist){
Write-Host "--------------------------"
Write-Host "$Ounames"
Write-Host "--------------------------"
$Results = @()
ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $Ounames.Name)){
$Results += Get-mailboxstatistics $Mailbox | Sort-Object TotalItemSize -Descending | Select @{label="User";expression=
{$
.DisplayName}}, <br> @{label=&quot;TotalSize&#40;MB&#41;&quot;;expression={$_.TotalItemSize.Value.ToMB&#40;&#41;}},
@{label="Items";expression={$.ItemCount}}, <br> @{label=&quot;Storage Limit&quot;;expression={$_.StorageLimitStatus}},
@{name="PrimarySMTPAddress";expression={$mailbox.PrimarySMTPAddress}}, `
Database,ServerName
}
$Results | Where { $
-ne $null } | ft -Autosize
}

Thanks for any input!!
by coderaven at 2013-04-02 12:02:22
Sort after you do your select-object by the attributes you create in that process. I would even recommend moving it after all of your other modifications like the where filter. The last thing you do would be the Sort-Object, otherwise the sort it lost.

$Results | Where-Object {…} | Sort-Object … | Format-Table …
by penguinviw at 2013-04-02 13:17:32
Thanks for the reply. I changed the code so that the sort-object was in the line you listed above, but it still does not work.
$Results | Where { $_ -ne $null } | Sort-Object TotalItemSize -Descending | ft -Autosize
Any ideas?
Thanks!!
by coderaven at 2013-04-02 13:32:03
In your total select, you remove totalitemsize and replace it with TotalSize(MB). Try sorting by that.
by penguinviw at 2013-04-02 16:41:28
The line now looks like:
$Results | Where { $_ -ne $null } | Sort-Object TotalSize(MB) -Descending |ft -Autosize
Now it only lists the OU names and nothing else. Very wierd.
by cmcknz77 at 2013-04-02 16:48:03
What do you get if you pipe $Results to Get-Member? i.e.:-
[code2=powershell]$Results | Where { $_ -ne $null } | Get-Member[/code2]
by cmcknz77 at 2013-04-02 16:59:08
or
[code2=powershell]$Results | Get-Member[/code2]

Additionally it might be worth taking the Brackets out of your ‘TotalSize(MB)’ label on your custom objects… Powershell tends to try to evaluate anything in parenthesis before the rest of the code so it might be worth you doing this instead:

[code2=powershell]$Results += Get-mailboxstatistics $Mailbox |
Select @{label="User";expression={$.DisplayName}},
@{label="TotalSize";expression={$
.TotalItemSize.Value.ToMB()}},
@{label="Items";expression={$.ItemCount}},
@{label="Storage Limit";expression={$
.StorageLimitStatus}},
@{name="PrimarySMTPAddress";expression={$mailbox.PrimarySMTPAddress}},
Database,
ServerName}

$Results | Where { $_ -ne $null } | Sort-Object TotalSize -Descending |ft -Autosize[/code2]

or enclosing the ‘TotalSize(MB)’ property like so:
[code2=powershell]$Results | Where { $_ -ne $null } | Sort-Object {TotalSize(MB)} -Descending |ft -Autosize[/code2]
by penguinviw at 2013-04-02 17:31:24
Thanks so much !!!
I followed your suggestion and changed "TotalSize(MB)" to "TotalSizeMB". That fixed it.
AWESOME!!! I been trying all kinds of things.
by cmcknz77 at 2013-04-03 00:10:21
Awesome… Happy to help… So this is solved?
by MasterOfTheHat at 2013-04-03 07:10:52
[quote="penguinviw"]The line now looks like:
$Results | Where { $_ -ne $null } | Sort-Object TotalSize(MB) -Descending |ft -Autosize
Now it only lists the OU names and nothing else. Very wierd.[/quote]
Wrap the column name in double quotes. That will tell posh to treat it as a string literal instead of an expression.
$Results | Where { $_ -ne $null } | Sort-Object "TotalSize(MB)" -Descending |ft -Autosize
Similar example:
Get-WmiObject win32_logicaldisk | Select-Object DeviceID, @{label="Free Space (MB)";expression={$_.FreeSpace / 1MB}} | Sort-Object "Free Space (MB)" | ft -AutoSize
Output:
DeviceID Free Space (MB)
-------- ---------------
G: 0
E: 0
D: 1686.484375
C: 70629.08984375
F: 793330.8359375