因此,我无法弄清楚如何让PowerShell显示一个表来比较2个用户的AD组成员资格。 脚本本身不需要进行任何比较,只需显示一个表格。用户名作为标题,下面的行是各自的组。
到目前为止我所有的变量都包含2个包含组列表的变量
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?
}
我确定有一种聪明的方式来做这个想法格式表,或创建一个二维数组,或哈希表或其他东西......我已经玩过这些我最了解,但没有任何输出会像:
SomeUser SomeOtherUser
-------- -------------
Admin Underpaid
Document Readers Document Readers
Document Writers Remote Workers
Office Workers
非常感谢任何建议。
答案 0 :(得分:2)
未经测试:
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++]
}
}
}
答案 1 :(得分:1)
试试这个,在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
}