【问题标题】:pscustomobject multiple lines per rowpscustomobject 每行多行
【发布时间】:2021-12-18 23:16:34
【问题描述】:

到目前为止,我有一些代码,但希望它为每个组的用户生成一个包含多行的表格。

目前,它会创建一个这样的表:

Group Users
abcgroup1 Alice
abcgroup1 Bob
abcgroup2 Bob
abcgroup2 Jason
abcgroup3 Eve

我希望它改为创建这样的表:

Group Users
abcgroup1 Alice
Bob
abcgroup2 Bob
Jason
abcgroup3 Eve
$Groups = get-adgroup -Filter 'name -like "*abc*"'

$Results = foreach( $Group in $Groups ){

    Get-ADGroupMember -Identity $Group | foreach {

        [pscustomobject]@{

            Group = $Group.Name

            Users = $_.Name

            }

        }

    }

$Results

$Results | Export-Csv C:\abc_search.txt -NoTypeInformation

【问题讨论】:

    标签: powershell csv active-directory-group pscustomobject


    【解决方案1】:

    您可以使用由 CRLF `r`n 连接的 -join 运算符。这将导致多行string

    $Groups = Get-ADGroup -Filter "name -like '*abc*'"
    $Results = foreach($Group in $Groups)
    {
        [pscustomobject]@{
            Group = $Group.Name
            Members = (Get-ADGroupMember -Identity $Group).Name -join "`r`n"
        }
    }
    
    $Results | Format-Table -Wrap -Autosize
    $Results | Export-Csv C:\abc_search.csv -NoTypeInformation
    

    请注意,我在Format-Table 上使用-Wrap,需要在控制台上正确显示多行strings

    您可以使用的另一个选项是 Out-String 认为还需要使用 .TrimEnd() 方法来摆脱尾随的新行:

    Members = ((Get-ADGroupMember -Identity $Group).Name | Out-String).TrimEnd()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多