So I can't figure out how to get PowerShell to display a table to compare 2 user's AD group memberships. The script doesn't have to do any comparison itself, simply display a table. Username as heading, and the rows underneath being their respective groups.
All I've got so far is 2 variables containing the groups lists
Function Show-Groups ($usr1,$usr2)
{
$groups1 = (((Get-ADUser $usr1 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samaccountname).samaccountname})
$groups2 = (((Get-ADUser $usr2 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samaccountname).samaccountname})
# Code to display in table?
}
I'm sure there's a clever way of doing this thought Format-Table, or creating a 2 dimensional array, or a hash table or something... I've played about with these the best I understand, but nothing that will output like:
SomeUser SomeOtherUser
-------- -------------
Admin Underpaid
Document Readers Document Readers
Document Writers Remote Workers
Office Workers
Any advice would be greatly appreciated.
Not tested:
Function Show-Groups ($usr1,$usr2)
{
$groups1 = (((Get-ADUser $usr1 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samaccountname).samaccountname})
$groups2 = (((Get-ADUser $usr2 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samaccountname).samaccountname})
$diff = $groups1.count - $groups2.count
if ($diff -gt 0){$groups2 += ,''*$diff}
else {$groups1 += ,''*-$diff}
$i=0
$groups1 |
foreach {
[PSCustomObject]@{
$Usr1 = $_
$Usr2 = $groups2[$i++]
}
}
}
Try this, tested successfully on PS2:
Function Show-Groups ($usr1,$usr2)
{
$groups1 = (((Get-ADUser $usr1 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samaccountname).samaccountname})
$groups2 = (((Get-ADUser $usr2 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samaccountname).samaccountname})
$arr=@()
$target1 = $groups1
$target2 = $groups2
if($groups2.Length -gt $groups1.Length)
{
$target1 = $groups2
$target2 = $groups1
}
for($i= 0; $i -lt $target1.Length;$i++)
{
$arr += , (New-Object pscustomobject -Property @{$usr1=$target1[$i];$usr2=$target2[$i]})
}
$arr
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.