Forum Discussion

Robert Bollinger's avatar
Robert Bollinger
Brass Contributor
Jul 06, 2020
Solved

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

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 

 

  • 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



  • GuidovanDijk's avatar
    GuidovanDijk
    Brass Contributor

    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



Resources