Arrays 创建两个用户的表';s广告组

Arrays 创建两个用户的表';s广告组,arrays,powershell,formatting,Arrays,Powershell,Formatting,所以我不知道如何让PowerShell显示一个表来比较2个用户的广告组成员资格。 脚本本身不必进行任何比较,只需显示一个表即可。用户名作为标题,下面的行是它们各自的组 到目前为止,我只得到了两个包含组列表的变量 Function Show-Groups ($usr1,$usr2) { $groups1 = (((Get-ADUser $usr1 -Prop memberof).memberof) | Foreach-Object {(Get-ADGroup $_ -Prop samacco

所以我不知道如何让PowerShell显示一个表来比较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
如有任何建议,将不胜感激。

未经测试:

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++]
     }
    }
}

请尝试此操作,并在PS2上成功测试:

函数显示组($usr1,$usr2)
{
$groups1=((Get ADUser$usr1-Prop memberof).memberof)| Foreach对象{(Get ADGroup$\ Prop samaccountname).samaccountname})
$groups2=((Get ADUser$usr2-Prop memberof).memberof)| Foreach对象{(Get ADGroup$\uprop-samaccountname).samaccountname})
$arr=@()
$target1=$groups1
$target2=$groups2
如果($groups2.Length-gt$groups1.Length)
{
$target1=$groups2
$target2=$groups1
}
对于($i=0;$i-lt$target1.Length;$i++)
{ 
$arr+=,(新对象pscustomobject-Property@{$usr1=$target1[$i];$usr2=$target2[$i]})
}
$arr
}

感谢您的输入。。。不幸的是,这会导致:名称和值标题,以及交替使用用户和组的行。我似乎无法在此处使用格式粘贴输出。是否以某种方式格式化此函数的结果?如果我简单地从@Mjolinor复制并粘贴上面的代码,并使用
Show Group test1 test2
调用它,我会得到每个组的组列表,标题为test1和test2.Positive。为了再次检查,我运行了一个干净的会话。复制mjolinor的代码并直接粘贴,并使用2个用户调用函数这是我在比较2个用户时得到的结果。(这张图片已经被模糊了,因为它是用于商业用途的)。这是使用PowerShell v2。[链接]如您所见,用户名在左侧交替出现,广告组(其中许多相同)在右侧列出,绝对完美。谢谢我正在研究pscustomobject方法,但还没有达到它实际工作的程度。现在我只需要看看你的代码,看看你做了什么,否则我什么都没学到。再次感谢!pscustomobject的大量使用