解析特定用户的 AD 组时出现问题

解析特定用户的 AD 组时出现问题

我有一个文件,其中列出了用户 ID,如下所示:

0012345
0345678

我将文件加载到数组中

$ary_UserList = @(Get-Content $src_user_list)

然后我对 ID 进行如下处理:

ForEach ($user in $ary_UserList){
    $userName = (Get-ADUser -identity "$user").Name
    $aryGroups = ((Get-ADUser -Filter {Name -like $userName} -properties memberof).memberof | Select-String -Pattern "MYGRP*")
        $DataRow = New-Object System.Object  #Create an object to hold the output fields
        $DataRow | Add-Member -MemberType NoteProperty -Name "User" -Value $user
        $DataRow | Add-Member -MemberType NoteProperty -Name "Name" -Value $userName
        $DataRow | Add-Member -MemberType NoteProperty -Name "ADGroup" -Value $aryGroups
$ary_GroupList += $DataRow 
    $DataRow | Format-Table |Out-String -Width 256
}
$ary_GroupList | Export-CSV -LiteralPath $outGroupFile -NoTypeInformation -Encoding UTF8

在 $DataRow | Format-Table |Out-String -Width 256 的屏幕上,我看到了这一点。

User    Name             ADGroup                                                                                        
----    ----             -------                                                                                        
0012345 Marian Bernedelo {CN=MYGRP-Managers,OU=Group Objects,DC=corp,DC=Company,DC=com, CN=MYGRP-Associates,OU=Group Objects,DC=corp,DC=Company,DC=com}

User    Name           ADGroup
----    ----           -------
0345678 Fanny Carlsbad CN=MYGRP-Associates,OU=Group Objects,DC=corp,DC=Company,DC=com

但 CSV 输出如下所示:

"User","Name","ADGroup"
"0012345","Marian Bernedelo","System.Object[]"
"0345678","Fanny Carlsbad","CN=MYGRP-Associates,OU=Group Objects,DC=corp,DC=Company,DC=com"

当有多个组时,如何将这些组放入 CSV 输出中?

谢谢!!

答案1

改变

$DataRow | Add-Member -MemberType NoteProperty -Name "ADGroup" -Value $aryGroups

PowerShell 7

$DataRow | Add-Member -MemberType NoteProperty -Name "ADGroup" -Value (($aryGroups -is [Aystem.Array]) ? $aryGroups -join '' : $aryGroups)

PowerShell 5

$DataRow | Add-Member -MemberType NoteProperty -Name "ADGroup" -Value @(($argroups -join ''), ($argroups))[!($argroups -is [System.Array])]

相关内容