Michael Leonard
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
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
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.
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
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
ASKER
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\Deskt op\add-to- groups.ps1 :4 char:29
+ Add-QADGroupMember -Identity <<<< $grp -Member $usr
+ CategoryInfo : InvalidData: (:) [Add-QADGroupMember], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Que st.ActiveR oles.ArsPo werShellSn apIn.Comma nds.AddGro up
MemberCmdlet2
/my input CSV file is attached.
pls advise
thx
list1.csv
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\Deskt
+ Add-QADGroupMember -Identity <<<< $grp -Member $usr
+ CategoryInfo : InvalidData: (:) [Add-QADGroupMember], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
MemberCmdlet2
/my input CSV file is attached.
pls advise
thx
list1.csv
ASKER
Hi Krzysztof, any suggestions to clear the error we are seeing?
much appreciated.
S.
much appreciated.
S.
ASKER
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
can you adust the script so that it will accpet sAMAccountName in that column?
thx
ASKER
does anyone else have a suggestion on this? would appreciate some assistance as we have a planned migration starting tonight.
thx.
S.
thx.
S.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Please specify there full group name to which you want to add users and would work fine
Krzysztof
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
ASKER
thx Gus. We are at win2k8 R2 AD DS
I'll test that in a bit.
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
}
ASKER
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.
many thanks!
S.
Open in new window
Regards,
Krzysztof