简体   繁体   中英

Adding Imported users into multiple groups

I have code to import users and modify their properties:

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 $_
  }

What I'm looking to do is add them to a second group in addition to the "Studentgroup" that is already working.

Simply do another Add-ADGroupMember :

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

You can use the SamAccountName of the groups, BTW. The distinguished name is not required.

With that said, if you want to add all new users to the same group, it would be better to collect the users in a variable, and then add them all in one go:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM