【问题标题】:Powershell - CSV Export formattingPowershell - CSV 导出格式
【发布时间】:2021-02-17 01:43:06
【问题描述】:

我正在尝试获取用户所属的 AD 组列表,并且我有一个有效的脚本。但我需要以不同的方式展示它。目前,该脚本在一行中显示一条记录,其中包含用户名,然后在下一个单元格中显示多个组名,以分号分隔。

我希望所有组都在每一行中并且用户名重复。

例子:

DavidChow - 服务器管理员组

DavidChow - 桌面管理员组

DavidChow - Azure 管理员组

NinaChow - 桌面用户组

这是我的脚本:

        $UnameList= Import-Csv C:\temp\users_full_list.csv
ForEach ($username in $UnameList){
$groups=Get-ADPrincipalGroupMembership -Identity $username.Identity | select -expand name
$data = [PSCustomObject]@{
          samaccountname = $username.Identity
          count = $groups.count
          memberOf = ($groups -join ';')}
 Write-Output $data  | Export-Csv C:\Temp\users_full_list_memberships.csv -Delimiter ";" -NoTypeInformation -Append
         }

【问题讨论】:

  • 我的回答有帮助吗?

标签: powershell csv export


【解决方案1】:

您可以为每个用户的每个组创建一个“数据”对象,然后在最后全部导出为 CSV:

Import-Csv C:\temp\users_full_list.csv | foreach {
    $identity = $_.Identity
    Get-ADPrincipalGroupMembership -Identity $identity | foreach {
        [PSCustomObject]@{
              SamAccountName = $identity
              GroupName = $_.Name
        }
    }
} | Export-Csv C:\Temp\users_full_list_memberships.csv -Delimiter ";" -NoTypeInformation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多