【发布时间】:2016-03-15 09:28:56
【问题描述】:
当我运行以下代码时:
'Windows Embedded Standard',
'Windows 7 Enterprise',
'Windows XP Professional',
'Windows Server 2003',
'Windows 7 Entreprise',
'',
'Windows 7 Professionnel',
'Windows 7 Professional',
'Windows 10 Enterprise',
'Windows Server 2008 R2 Standard',
'Windows Server 2012 Standard',
'Windows Server 2012 R2 Standard',
'unknown',
'Windows Server 2008 R2 Enterprise' | Sort-Object
排序如下:
unknown
Windows 10 Enterprise
Windows 7 Enterprise
Windows 7 Professional
Windows 7 Professionnel
Windows Embedded Standard
Windows Server 2003
Windows Server 2008 R2 Enterprise
Windows Server 2008 R2 Standard
Windows Server 2012 R2 Standard
Windows Server 2012 Standard
Windows XP Professional
Windows 7 Entreprise
我不明白为什么最后一个条目Windows 7 Entreprise 没有正确排序。它应该排在第四位。我在这里错过了什么?
更新
感谢 cmets,很明显源数据似乎是问题所在:
$Computers = Get-ADComputer -SearchBase 'OU=EU,DC=domain,DC=net' -Filter * -Properties OperatingSystem
$Computers | Group-Object OperatingSystem | Sort-Object Name | Select-Object Count, Name | Format-Table -AutoSize
ANSI 值:
$Computers | Group-Object OperatingSystem | ForEach-Object{$_.Name;[int[]][char[]]$_.Name -join "|"}
Windows Server 2008 R2 Standard
87|105|110|100|111|119|115|32|83|101|114|118|101|114|32|50|48|48|56|32|82|50|32|83|116|97|110|100|97|114|100
Windows Server 2003
87|105|110|100|111|119|115|32|83|101|114|118|101|114|32|50|48|48|51
Windows Server 2012 Standard
87|105|110|100|111|119|115|32|83|101|114|118|101|114|32|50|48|49|50|32|83|116|97|110|100|97|114|100
Windows Server 2012 R2 Standard
87|105|110|100|111|119|115|32|83|101|114|118|101|114|32|50|48|49|50|32|82|50|32|83|116|97|110|100|97|114|100
unknown
117|110|107|110|111|119|110
Windows Server 2008 R2 Enterprise
87|105|110|100|111|119|115|32|83|101|114|118|101|114|32|50|48|48|56|32|82|50|32|69|110|116|101|114|112|114|105|115|101
Windows 7 Enterprise
87|105|110|100|111|119|115|32|55|32|69|110|116|101|114|112|114|105|115|101
Windows 7 Entreprise
87|105|110|100|111|119|115|160|55|32|69|110|116|114|101|112|114|105|115|101
Windows 7 Professionnel
87|105|110|100|111|119|115|32|55|32|80|114|111|102|101|115|115|105|111|110|110|101|108
Windows XP Professional
87|105|110|100|111|119|115|32|88|80|32|80|114|111|102|101|115|115|105|111|110|97|108
Windows 7 Professional
87|105|110|100|111|119|115|32|55|32|80|114|111|102|101|115|115|105|111|110|97|108
Windows 10 Enterprise
87|105|110|100|111|119|115|32|49|48|32|69|110|116|101|114|112|114|105|115|101
Windows Embedded Standard
87|105|110|100|111|119|115|32|69|109|98|101|100|100|101|100|32|83|116|97|110|100|97|114|100
有没有办法对它进行不同的编码,以便 PowerShell 可以正确排序?
【问题讨论】:
-
我刚刚在本地运行了类似的操作,并且排序正确。你用的是什么版本的powershell?
-
PSVersion 4.0在 Windows Server 2012 上。 -
使用 PS 4 在我的 Server 2012 上也能正确排序。
-
也许您的源数据有问题?也许是一些不可见的 unicode 字符?你能在十六进制编辑器中检查脚本吗?
-
Luaan,你在做点什么。当我从这里的网站复制代码时,它确实运行正常。但不是当我从源数据中复制它时,即
Get-ADComputer -SearchBase 'OU=EU,DC=domain,DC=net' -Filter * -Properties OperatingSystem
标签: sorting powershell encoding active-directory powershell-4.0