Link to home
Start Free TrialLog in
Avatar of Michael Leonard
Michael LeonardFlag for United States of America

asked on

script to add users to groups - bulk?

can someone provide a powershell script that will allow me to key on an input CSV file and add members to the appropriate groups.

for example the CSV would be structured like this:

column A                               Column B
jsmith                                     engineering-group
bsmith                                    engineering-group
dsmith                                    engineering-group
adavid                                     marketing-group
bdavid                                     marketing-group
cdavid                                     marketing-group .... etc
Avatar of Krzysztof Pytko
Krzysztof Pytko
Flag of Poland image

Try this way
Import-CSV c:\list.csv | %{
$usr=Get-QADUser $_."columnA"
$grp=Get-QADGroup $_."columnB"
Add-QADGroupMember -Identity $grp -Member $usr
}

Open in new window


Regards,
Krzysztof
Avatar of Michael Leonard

ASKER

Hi Krzysztof, just tested in the lab and here is the error:

Add-QADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argumen
t that is not null or empty and then try the command again.
Hello,

hm strange it works for me. How did you prepare your CSV file ? It should be in format

columnA,columnB
user1,group1
user2,group2

comma separated values

Krzysztof
hi .. yes just re-verfied that is exactly what i have in my .csv file.

here is the full error i get when running it as .ps1 script

Add-QADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Supply an argumen
t that is not null or empty and then try the command again.
At C:\Users\my-acccount\Desktop\add-to-groups.ps1:4 char:29
+ Add-QADGroupMember -Identity <<<<  $grp -Member $usr
    + CategoryInfo          : InvalidData: (:) [Add-QADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Commands.AddGroup
   MemberCmdlet2


/my input CSV file is attached.

pls advise

thx
list1.csv
Hi Krzysztof, any suggestions to clear the error we are seeing?

much appreciated.

S.
Krzysztof, I think its failing because i have the sAMAccountName in columnA.

can you adust the script so that it will accpet sAMAccountName in that column?

thx
does anyone else have a suggestion on this?  would appreciate some assistance as we have a planned migration starting tonight.

thx.

S.
ASKER CERTIFIED SOLUTION
Avatar of GusGallows
GusGallows
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I went home. From your CSV file, looks like columnB - group is incomplete and Get-QADGroup finds more than one (qs_)

Please specify there full group name to which you want to add users and would work fine

Krzysztof
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm ok, I was going off the first post. Just saw your list.csv. The column headers are correct. I don't see anything missing from column b unless those aren't the correct names. What version of AD are you on?
If you have the AD module, you can do it this way:
Import-Module ActiveDirectory
Import-CSV c:\list.csv | %{
$usr=Get-ADUser $_."columnA"
$grp=Get-ADGroup $_."columnB"
Add-ADGroupMember -Identity $grp -Member $usr
} 

Open in new window

thx Gus.  We are at win2k8 R2 AD DS
I'll test that in a bit.
If it still doesn't work, try converting to the distinguished name from the SamAccountName. You can do it like this:
Import-Module ActiveDirectory
Import-CSV c:\list.csv | %{
$usr=Get-ADUser $_."columnA"
$usrdg = $usr.distinguishedName
$grp=Get-ADGroup $_."columnB"
$grpDG = $grp.distinguishedName
Add-ADGroupMember -Identity $grpDG -Member $usrdg
} 

Open in new window

thanks guys, the problem was on my side, i had the group names added incorrectly. both of the logic provided worked like a charm.

many thanks!

S.