将导入的用户添加到多个组中

时间:2016-03-23 09:27:24

标签: powershell active-directory

我有导入用户并修改其属性的代码:

Import-Csv users.csv |
  select Surname, @{n='GivenName';e={$_.'FirstName'}},
         @{n='samaccountname';e={$_.FirstName.substring(0,2) + $_.Surname}},
         @{n='UserPrincipalName';e={$_.FirstName.substring(0,2) + $_.Surname}},
         @{n='Name';e={$_.'FirstName' + ' ' + $_.'Surname'}} |
  New-ADUser -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText "Password1" -Force) -ChangePasswordAtLogon $true -Path "OU=Intake 20XX,OU=Students,OU=Ravenloft users,DC=RAVENLOFT,DC=test" -ProfilePath {"\\TESTSVR\Profiles$\Intake20XX\" +$_.SamAccountName} -HomeDrive "D:" -HomeDirectory {"\\TESTSVR\Work$\Intake20XX\" +$_.SamAccountName} -PassThru |
  Foreach-Object {
    Add-ADGroupMember -Identity "CN=StudentGroup,OU=Students,OU=Ravenloft users,DC=RAVENLOFT,DC=test" -Members $_
  }

除了"学生组"我还希望将其添加到第二组。已经有效了。

1 个答案:

答案 0 :(得分:1)

只需做另一个Add-ADGroupMember

Foreach-Object {
  Add-ADGroupMember -Identity 'StudentGroup' -Members $_
  Add-ADGroupMember -Identity 'othergroup' -Members $_
}

您可以使用BT的SamAccountName组。专有名称不是必需的。

话虽如此,如果你想将所有新用户添加到同一个组,最好在变量中收集用户,然后一次性添加它们:

$users = Import-Csv users.csv | ... | New-ADUser ... -PassThru
Add-ADGroupMember -Identity 'StudentGroup' -Members $users
Add-ADGroupMember -Identity 'othergroup' -Members $users