根据CSV中的标头将用户列表添加到AD组

时间:2017-09-27 08:31:06

标签: powershell csv active-directory

我正在尝试制作一个脚本,根据标题将用户添加到AD组,CSV如下所示:

Group1  Group2  Group3
UserA   UserC   UserA
UserB           UserC

我找到的最接近的脚本如下,但这基于以下格式,这有点不同......

Group             Accountname
group1            user1
group1            user2
group1            user3
Import-Module ActiveDirectory
$list = Import-Csv ".\Bulk_Import.csv"

foreach ($user in $list) {
    Add-ADGroupMember -Identity $user.Group -Member (Get-ADUser $user.Accountname)
}

脚本是否可以根据我的格式添加用户,如果用户已经在组中,则忽略错误?

1 个答案:

答案 0 :(得分:0)

Import-Module ActiveDirectory
$list = Import-Csv ".\Bulk_Import.csv"

ForEach ($Entry in $List) {
    $Entry.PSObject.Properties | Where {$Entry.($_.Name)} | ForEach {
        Add-ADGroupMember -Identity $_.Name -Member (Get-ADUser $Entry.($_.Name))
    }
}