May 01 2018
08:47 AM
- last edited on
Feb 06 2023
03:19 AM
by
TechCommunityAP
May 01 2018
08:47 AM
- last edited on
Feb 06 2023
03:19 AM
by
TechCommunityAP
I have a scenario where i need to take input from the CSV file for multiple users who have their UserprincipalName, Security Group and Action (Add or Remove).I need to write a power shell script to add the users automatically after taking the inputs from the CSV file.
UserPrincipalName, Secuirty Group, Action
test1,G1,Add
Test2,G2,Add .... ....
Tes20,G12,Remove
I used this for single user
Connect-MsolService
$groups = Get-MsolGroup -all -grouptype security | where {$_.DisplayName -eq "G1"}
$users = Get-MsolUser -all | where {$_.UserPrincipalName -like "Test*"}
$users | Foreach {Add-MsolGroupMember -GroupObjectId $group.objectid -GroupMemberType “user” -GroupMemberObjectId $_.objectid}
How would you do it for the above scenario for multiple users?
May 01 2018 10:48 AM
I'd start by separating the list to two CSVs, one for "add" and one for "remove". Then, I'd replace the group name with a better, unique identifier, as using attributes such as "display name" is a receipt for disaster. Use the objectId of the group, or if it's a mail-enabled one, the SMTP address. Same goes for the user, use property such as the UserPrincipalName.
There are many such script samples available online, just download one and modify it do better suit your needs.
May 01 2018 11:52 AM
Thanks for the reply.
Can please direct me to one of those sample scripts which can be used.