I would like to get a computer list from the AD with the groups the computers are members in. So far fairly easy:
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.
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
e.g. somthing like this:
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")? What would be a working approach?
CodePudding user response:
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. If this is meant for exporting the data, this approach is pointless given that CanonicalName
is a string
value and MemberOf
is an array
:
$computers = @(
[pscustomobject]@{
CanonicalName = 'somedomain.com/Computers/Computer2'
Name = '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'
Name = '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 Name, @{
Name = 'MemberOf'
Expression = { $_.MemberOf | Sort-Object }
}
}
Name MemberOf
---- --------
Computer1 {CN=(A) Group1,OU=OU-Path1,OU=OU-Path2,DC=domain,DC=local, CN=(A) Group2,....
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:
$computers | ForEach-Object {
foreach($group in $_.MemberOf)
{
$cn, $ou = $group -split '(?<!\\),',2
[pscustomobject]@{
CanonicalName = $_.CanonicalName
Name = $_.Name
MemberOf = $cn.Replace('CN=','')
OU = $ou
}
}
} | Sort-Object CanonicalName, MemberOf | Select-Object Name, MemberOf
Name MemberOf
---- --------
computer1 (A) Group1
computer1 (A) Group2
computer1 (A) Group3
computer1 (A) Group4
computer2 (A) Group1
computer2 (A) Group2
computer2 (A) Group3
computer2 (A) Group4