如何以正确的格式获取 Get-ADPrincipalGroupMembership 名称?

How to fetch Get-ADPrincipalGroupMembership name in proper format?

我的 PowerShell 脚本是这样的:

Get-ADUser -Property * -filter * | Select-Object @{Label="Member Of";Expression={
  %{Get-ADPrincipalGroupMembership -Identity $_.SamAccountName | Select-Object name}
}}

输出:

Member Of
---------
{@{name=XYZUsers}, @{name=GLOBAL_XYZ}, @{name=LOCAL_XYZ}, @{name=ABC_APPROVER}
{@{name=XYZUsers}, @{name=GLOBAL_XYZ}, @{name=LOCAL_XYZ}, @{name=ABC_APPROVER}

我希望输出是这样的:

Member Of
---------
XYZUsers, GLOBAL_XYZ, LOCAL_XYZ
XYZUsers, GLOBAL_XYZ, LOCAL_XYZ

我想Select-Object-ExpandProperty参数可能就是你需要的

试试这个:

Get-ADUser -property * -filter * | select-object @{Label = "Member Of";Expression = {%{Get-ADPrincipalGroupMembership -Identity $_.SamAccountName | Select-Object -ExpandProperty name}}}

或者,我认为您也可以将代码简化为这样,但我现在没有地方可以对其进行测试:

Get-ADUser -property * -filter * | % {Get-ADPrincipalGroupMembership -Identity $_.SamAccountName | Select-Object name}

您的 Select-Object 语句为您提供带有 属性 name (@{name=...}) 的自定义对象。您需要提取此 属性 的值(例如,通过 Select-Object-ExpandProperty 参数),然后将提取的组名称加入字符串:

Get-ADUser -Property * -Filter * | Select-Object @{n='Member Of';e={
  (Get-ADPrincipalGroupMembership -Identity $_.SamAccountName |
    Select-Object -Expand Name) -join ', '
}}

get-aduser -属性 * -filter * | select-object @{Label = "Member Of";Expression = {%{Get-ADPrincipalGroupMembership -Identity $.SamAccountName |foreach-Object {$.name}} }}