Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the script to assign permission to managed identity to Msft Graph #6

Open
SuryenduB opened this issue Jan 7, 2025 · 0 comments

Comments

@SuryenduB
Copy link

SuryenduB commented Jan 7, 2025

$ManagedIdentityName = 'LA-Emergency-Revoke-Access-OnPrem'
$GraphAppId = '00000003-0000-0000-c000-000000000000' # Don't change this. This is the immutable application ID of the Microsoft Graph service principal.
$DestinationTenantId = 'XX-XX-XX' 
$AssignMgPermissions = @(
    'User.Read.All'
    'User.ReadWrite.All' 
    'AuditLog.Read.All',
    'GroupMember.ReadWrite.All',
    'Directory.Read.All',
    'Device.ReadWrite.All',
    'DeviceManagementManagedDevices.PrivilegedOperations.All',
    'DeviceManagementManagedDevices.Read.All',
    'UserAuthenticationMethod.ReadWrite.All',
    'SynchronizationData-User.Upload' 
)

# Scopes required to perform Graph permission assignment
$MgRequiredScopes = @(
    'Application.Read.All'
    'AppRoleAssignment.ReadWrite.All'
    'Directory.Read.All'
    
)
# Connect to Graph and ExchangeOnline Powershell
Connect-MgGraph -TenantId $DestinationTenantId -Scopes $MgRequiredScopes #-NoWelcome #Uncomment NoWelcome if desired

# Get the SP objects
$MIdSPN = Get-MgServicePrincipal -Filter "displayName eq '$ManagedIdentityName'"
$GraphSPN = Get-MgServicePrincipal -Filter "appId eq '$GraphAppId'"


# Retrieve MS Graph App role Ids for your specified permissions
$AppPermIds = $GraphSPN.AppRoles | Where-Object { ($_.Value -in $AssignMgPermissions) -and ($_.AllowedMemberTypes -contains 'Application') }

# Iteravely assign the permissions to the Managed Identity
foreach ($AppPermission in $AppPermIds) {
    $AppPermissionAssingment = @{
        'PrincipalId' = $MIdSPN.Id
        'ResourceId'  = $GraphSPN.Id
        'AppRoleId'   = $AppPermission.Id
    }
  
    New-MgServicePrincipalAppRoleAssignment `
        -ServicePrincipalId $AppPermissionAssingment.PrincipalId `
        -BodyParameter $AppPermissionAssingment `
        -Verbose
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant