简体   繁体   English

通过 Powershell 对多个 AD 对象属性进行排序

[英]Sorting of multiple AD-object attributes via Powershell

I would like to get a computer list from the AD with the groups the computers are members in. So far fairly easy:我想从 AD 中获得一个计算机列表,其中包含计算机所属的组。到目前为止相当容易:

Get-adcomputer -filter * -properties Memberof | Sort CanonicalName | select-object -Property Name, Memberof

Works fine but the problem is that the groups (members of attribute Memberof ) are being listet unsorted and the sorting is inconsistent from object to object.工作正常,但问题是组(属性Memberof的成员)未排序,并且从 object 到 object 的排序不一致。

Name      Memberof                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
----      --------  
PC001     {CN=(A) Group3,OU=OU-Path,DC=domain,DC=local, CN=(A) Group1,OU=OU-Path,DC=domain,DC=local, CN=(A) Group4,OU=OU-Path,DC=domain,DC=local, CN=(A) Group2,OU=OU-Path,DC=domain,DC=local}
PC002     {CN=(A) Group4,OU=OU-Path,DC=domain,DC=local, CN=(A) Group3,OU=OU-Path,DC=domain,DC=local, CN=(A) Group2,OU=OU-Path,DC=domain,DC=local, CN=(A) Group3,OU=OU-Path,DC=domain,DC=local}
PC003     ...     

I tried to get things sorted out based on my previous question using -ExpandProperty MemberOf eg somthing like this:我尝试使用-ExpandProperty MemberOf根据我之前的问题来解决问题,例如:

Get-adcomputer -filter * -properties Memberof | Sort CanonicalName | select-object -Property Name -ExpandProperty MemberOf | Sort-Object -Property MemberOf

Which I'm not surprised, did not work.我并不感到惊讶,没有工作。 Is is that possible in that manner or do I have to read out the Name and the MemberOf into separate variables and combine them again afterwards (and how do I make sure they "allign")?以这种方式可能吗,还是我必须将 Name 和 MemberOf 读出到单独的变量中,然后再将它们组合起来(以及如何确保它们“对齐”)? What would be a working approach?什么是工作方法?

I believe the only way around this would be to construct a new object altogether however I don't see the point on doing this other than displaying the objects on the console.我相信解决这个问题的唯一方法是完全构建一个新的 object 但是除了在控制台上显示对象之外,我没有看到这样做的意义。 If this is meant for exporting the data, this approach is pointless given that CanonicalName is a string value and MemberOf is an array :如果这是为了导出数据,那么这种方法毫无意义,因为CanonicalName是一个string值并且MemberOf是一个array

$computers = @(
    [pscustomobject]@{
        CanonicalName = 'somedomain.com/Computers/Computer2'
        MemberOf      = @(
            'CN=(A) Group3,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
            'CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
            'CN=(A) Group4,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
            'CN=(A) Group2,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
        )
    }
    [pscustomobject]@{
        CanonicalName = 'somedomain.com/Computers/Computer1'
        MemberOf      = @(
            'CN=(A) Group3,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
            'CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
            'CN=(A) Group4,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
            'CN=(A) Group2,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local'
        )
    }
)

$computers | Sort-Object CanonicalName | ForEach-Object {
    $_ | Select-Object CanonicalName, @{
        Name = 'MemberOf'
        Expression = { $_.MemberOf | Sort-Object }
    }
}
CanonicalName                      MemberOf
-------------                      --------
somedomain.com/Computers/Computer1 {CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local, CN=(A) Group2,....
somedomain.com/Computers/Computer2 {CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local, CN=(A) Group2,....

If this was meant for exporting the data to, for example a CSV, this would be the approach you should take:如果这是为了将数据导出到例如 CSV,那么您应该采用以下方法:

$computers | ForEach-Object {
    foreach($group in $_.MemberOf)
    {
        [pscustomobject]@{
            CanonicalName = $_.CanonicalName
            MemberOf      = $group
        }
    }
} | Sort-Object CanonicalName, MemberOf
CanonicalName                      MemberOf
-------------                      --------
somedomain.com/Computers/Computer1 CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer1 CN=(A) Group2,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer1 CN=(A) Group3,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer1 CN=(A) Group4,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer2 CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer2 CN=(A) Group2,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer2 CN=(A) Group3,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local
somedomain.com/Computers/Computer2 CN=(A) Group4,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local

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

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