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
siber1Asked:
Who is Participating?
 
GusGallowsConnect With a Mentor Commented:
There is a typo in the code. Your column headers have a space in them. The code doesn't. Try the following:
Import-CSV c:\list.csv | %{
$usr=Get-QADUser $_."column A"
$grp=Get-QADGroup $_."column B"
Add-QADGroupMember -Identity $grp -Member $usr
} 

Open in new window


Basically, you are passing it blank values because the columns called ColumnA and ColumnB do not exist.
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
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
0
 
siber1Author Commented:
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.
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
Krzysztof PytkoSenior Active Directory EngineerCommented:
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
0
 
siber1Author Commented:
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
0
 
siber1Author Commented:
Hi Krzysztof, any suggestions to clear the error we are seeing?

much appreciated.

S.
0
 
siber1Author Commented:
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
0
 
siber1Author Commented:
does anyone else have a suggestion on this?  would appreciate some assistance as we have a planned migration starting tonight.

thx.

S.
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
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
0
 
GusGallowsConnect With a Mentor Commented:
Also, depending on your version of active directory, if on 2008 R2, you can do the following with the AD module instead of QAD:
Import-CSV c:\list.csv | %{
$usr=Get-ADUser $_."column A"
$grp=Get-ADGroup $_."column B"
Add-ADGroupMember -Identity $grp -Member $usr
} 

Open in new window

0
 
GusGallowsCommented:
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?
0
 
GusGallowsCommented:
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

0
 
siber1Author Commented:
thx Gus.  We are at win2k8 R2 AD DS
I'll test that in a bit.
0
 
GusGallowsCommented:
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

0
 
siber1Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.