【问题标题】:powershell sort-object doesn't work as expectedpowershell sort-object 无法按预期工作
【发布时间】:2013-01-29 13:38:23
【问题描述】:

我尝试使用 sort-object cmdlet 在这里按 ID 对进程进行排序:

Get-Process | Sort-Object -Property Id

而且效果很好。在我发现的任何其他示例中,排序工作正常,但是当我尝试使用此单行从 Active Directory 中按员工 ID 对员工进行排序时:

Get-QADUser -IncludeAllProperties -SerializeValues | ? {?_.Mail} | select employeeID | sort-object -property employeeID

我得到这样的东西:

11 1104 1105 1185 119 12 ...

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 quest


    【解决方案1】:

    Get-QADUser 将 eployeeId 作为字符串返回,因此 sort 使用字符串排序机制。 要将employeeIds 排序为整数 - 只需将属性转换为这种类型:

    Get-QADUser -IncludeAllProperties | Sort-Object { [int]$_.employeeId } | select Name, employeeid 
    

    【讨论】:

      【解决方案2】:

      你也可以使用 {$_.employeeId -as [int]}。这不会导致 null 错误。

      我用“Frode F”来解决这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-06-17
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多