SOLVED

Add multiple users to a list of Admin Roles Groups, PowerShell.

Brass Contributor

Hey Guys, 

 

Hoping you can assist here. I am trying to add a list of  users who are currently members of a Security Group to several different Administrative Role's. 

 

$Users = Get-Msolgroup -All | Where-Object {$_.DisplayName -eq "Health and Safety 2"}
Get-MsolGroupMember -GroupObjectId $Users.ObjectId
$Roles = @("Exchange Service Administrator", "Sharepoint Service Administrator", "Helpdesk Administrator")
foreach ($User in $Users)
{
Add-MsolRoleMember -RoleMemberEmailAddress $Users.ObjectId -RoleName $Roles
}

 

This is the error: Add-MsolRoleMember : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'RoleName'. Specified method is not supported.

 

Any ideas on how i can get the above to work? 

 

Thanks, 

 

Robert 

 

2 Replies
best response confirmed by VI_Migration (Silver Contributor)
Solution

Hi @Robert Bollinger 
After reading your script I saw a few issues
The first is $users the result you get is not the user, but only the group
Next when you read the documentation regarding the Add-MsolRoleMember  there are 2 parameters available for adding a user (RoleMemberEmailAddress  and -RoleMemberObjectId)

I changed your script 

 
$group = Get-Msolgroup -All | Where-Object {$_.DisplayName -eq "Health and Safety 2"}
$users = Get-MsolGroupMember -GroupObjectId $group.ObjectId
$Roles = @("Exchange Service Administrator""Sharepoint Service Administrator""Helpdesk Administrator")
foreach ($role in $roles )
{
    foreach ($user in $users){
        Add-MsolRoleMember -RoleName $role -RoleMemberObjectId $User.ObjectId
    }
}

Hope this solves your issue
Regards
Guido



@Guido van Dijk 

 

Thanks Guido!! That did it!. 

 

Robert

1 best response

Accepted Solutions
best response confirmed by VI_Migration (Silver Contributor)
Solution

Hi @Robert Bollinger 
After reading your script I saw a few issues
The first is $users the result you get is not the user, but only the group
Next when you read the documentation regarding the Add-MsolRoleMember  there are 2 parameters available for adding a user (RoleMemberEmailAddress  and -RoleMemberObjectId)

I changed your script 

 
$group = Get-Msolgroup -All | Where-Object {$_.DisplayName -eq "Health and Safety 2"}
$users = Get-MsolGroupMember -GroupObjectId $group.ObjectId
$Roles = @("Exchange Service Administrator""Sharepoint Service Administrator""Helpdesk Administrator")
foreach ($role in $roles )
{
    foreach ($user in $users){
        Add-MsolRoleMember -RoleName $role -RoleMemberObjectId $User.ObjectId
    }
}

Hope this solves your issue
Regards
Guido



View solution in original post