简体   繁体   English

使用PowerShell按邮箱大小对csv out放置文件进行降序排序

[英]Sort csv out put file in desending by Mailbox size with PowerShell

i have a script which is working without any problem, my only issue is that i can't order the output as i needed. 我有一个可以正常工作的脚本,我唯一的问题是我无法按需订购输出。

I've tried to remove the Sort-object CmdLet but it didn't change any thing, also I've tried to "play" with diffrient of variation of the Sort-object CmdLet. 我尝试删除了排序对象CmdLet,但是它没有任何改变,我也尝试了“播放”排序对象CmdLet的各种变化。

here is the code: 这是代码:

  $users = Import-Csv "C:\CSV Test\Alias_test.csv"

  foreach ($user in $users) {

  Get-MailboxStatistics -Identity $user.UserName |

   Select-Object @{l="User Name";e="DisplayName"},

   @{l="Mail Box Size";e="TotalItemSize"}, 

   @{l="TotalEmails?";e="itemcount"} |

   Sort-Object -Property ItemCount|

   Export-Csv "C:\CSV Test\Mailboxe_size.csv" -Append -NoTypeInformation

                             } #end of ForEach loop

The csv file looks like that: csv文件如下所示:

顺序错误

Thanks alot for your help 非常感谢你的帮助

There are two problems with your current script. 当前脚本有两个问题。

First is that each users mailbox statistics are sorted individually, so move the Sort-Object statement outside of the loop. 首先是每个用户的邮箱统计信息都是单独排序的,因此请将Sort-Object语句移出循环。

Second problem is that by the time you sort the objects, the ItemCount property no longer exists, since you've renamed it to TotalEmails? 第二个问题是,当您对对象进行排序时, ItemCount属性不再存在,因为您已将其重命名为TotalEmails? , so make sure you update the Property argument: ,因此请确保您更新Property参数:

$users = Import-Csv "C:\CSV Test\Alias_test.csv"

$stats = foreach ($user in $users) {
    Get-MailboxStatistics -Identity $user.UserName |
    Select-Object @{l="User Name";e="DisplayName"},
    @{l="Mail Box Size";e="TotalItemSize"}, 
    @{l="TotalEmails?";e="itemcount"} 
} #end of ForEach loop

$Stats |Sort-Object -Property TotalEmails? |Export-Csv "C:\CSV Test\Mailboxe_size.csv" -Append -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM