PowerShell - 如何将带有数组的行分成多行?

PowerShell - 如何将带有数组的行分成多行?

我正在编写一个 PowerShell 脚本来导出用户信息,包括用户所属的 SharePoint 组。部分脚本如下所示:

$web = Get-SPWeb $webURL
$users = Get-SPUser -Web $web.Url
foreach($user in $users){
   $staffObject = New-Object PSObject -Property @{
                'UserID' = $user.LoginName
                'Groups' = $user.Groups
                'Join Date' = ""
                'Referal' = ""
            }
$resultsarray += $staffObject 
}

$resultsarray | Select UserID,@{Name="Groups";Expression={$_."Groups" -join '|'}},'Join Date','Referral' | Export-csv -path $sFileLocation -Append -NoTypeInformation

现在输出如下:

"UserID","Groups","Join Date","Referal"
"123","Group A|Group B|Group C","",""

我怎样才能更改为:

"UserID","Groups","Join Date","Referal"
"123","Group A","",""
"123","Group B","",""
"123","Group C","",""

非常感谢。

答案1

这有帮助吗?

$CSV = Import-Csv C:\Users\Ryan\Downloads\test.txt
$ReformattedUsers = @()
Foreach ($User In $CSV)
{
    Foreach ($Group In $User.Groups.Split('|'))
    {
        $ReformattedUsers += [PSCustomObject]@{ 'UserID' = $User.UserID; `
                                                'Groups' = $Group; `
                                                'Join Date' = $User.'Join Date'; `
                                                'Referal (sic)' = $User.Referal  }
    }
}

$ReformattedUsers | Format-Table -AutoSize

$ReformattedUsers | Export-Csv C:\Users\Ryan\Downloads\out.csv -NoTypeInformation

输出:

UserID Groups  Join Date Referal (sic)
------ ------  --------- -------------
123    Group A                        
123    Group B                        
123    Group C                        

生成的 CSV 如下所示:

"UserID","Groups","Join Date","Referal (sic)"
"123","Group A","",""
"123","Group B","",""
"123","Group C","",""

相关内容