我的CSV文件如下所示:
Username,Groups
User01,Group01;Group02;Group03
User02,Group10;Group24;Group08
User03,Group13;Group02;Group42
等等......总共有数百个用户和数百个不同的群组,因此手动将个人用户和/或群组拆分为自己的行并不是一种选择。
我无法弄清楚如何使用foreach处理行,同时使用不同的分隔符拆分导入的CSV 两次,然后再次按每个用户分组。任何想法..?
答案 0 :(得分:1)
您可以在;
字符上拆分组,因此如果您想要将用户添加到组,请使用以下命令:
$src = Import-Csv yourfile.csv
foreach($line in $src){
$user = $line.Username
$groups = $line.Groups -split ";"
foreach($group in $groups){
Add-ADGroupMember -Identity $group -Members $user
# other code which uses $group and $user
}
}
答案 1 :(得分:0)
你可能想要像
这样的东西$Line = import-csv yourfile.csv
#Skip first line here
foreach ($Data in $Line)
{
$User = $Data.v1
$Group1 = $Data.v2
$Group2 = $Data.v3
$Group3 = $Data.v4
write-host "User: "$User
write-host "Group1: "$Group1
write-host "Group2: "$Group2
write-host "Group3: "$Group3
Write-Host ""
}