> For the complete documentation index, see [llms.txt](https://docs.intuneassistant.cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.intuneassistant.cloud/extensions/worker/add-worker-permissions.md).

# Add worker permissions

To grand the corrects permissions to the created worker follow the steps below.

## Grant the permissions from the Worker page

A Worker installed from the Azure Marketplace waits for its permissions before it asks Azure for any token. Open the Worker in the portal (**Worker**, then the Worker itself) and use **Grant this Worker its permissions**:

1. The page lists the roles the Worker needs: the ones every Worker needs and those of the services your tenant has, such as Security Insights. The Defender for Endpoint roles are marked **Optional**: they only exist in a tenant with a Defender for Endpoint license, and without them Security Insights leaves the device and vulnerability counters out. Tick them only if you have the license.
2. **Grant** opens a Microsoft sign-in window and asks for two Microsoft Graph permissions (`AppRoleAssignment.ReadWrite.All` and `Application.Read.All`). They stay in your browser tab and are not sent to us. A Global Administrator can approve them for the organization in that window.
3. The account you sign in with needs the **Privileged Role Administrator** or **Global Administrator** role in the tenant the Worker runs in. An Application Administrator can not assign Microsoft Graph roles. For a customer you manage, sign in with an account of that customer's tenant.
4. Only the roles on the page are added, and nothing is removed. A role that is already granted is left as it is.

Because the Worker has asked for no token until now, the first token it gets already holds the roles: it starts working within a minute. If a role is added later to a Worker that already holds a token, Azure only shows it in the token after that token expires (up to about a day); the page tells you from when. Restarting the Worker does not change this.

If you cannot use the page, use the script below. It does the same. A Worker that waits for its permissions cannot see that you ran the script, so open the Worker page afterwards and choose **I already granted them another way**; the Worker then starts. What you state there is not checked: the Worker's own permission check shows what is still missing.

For a service that comes with a Worker module, such as Security Insights, the **Setup** page of that service builds the script for you and shows which permissions are still missing; see [Setup](https://docs.intuneassistant.cloud/intune-assistant/security-insights/worker-setup.md). The roles below are the Worker's own base roles, set up once whichever modules use the Worker; the roles a module adds on top are on that module's page under [Modules](https://docs.intuneassistant.cloud/intune-assistant/modules.md).

Copy the script below into a file, save the file as add-permission

{% stepper %}
{% step %}

### Copy code and save file.

Copy the code and save the file to grand-worker-permissions.ps1

```ps1
#!/usr/bin/env pwsh
<#
.SYNOPSIS
    Assigns Microsoft Graph application permissions to a Worker's managed identity.

.DESCRIPTION
    Uses Microsoft.Graph.Authentication module for interactive browser login (supports
    Conditional Access / MFA). All Graph calls use Invoke-MgGraphRequest.

.PARAMETER TenantId
    Your Azure AD Tenant ID.

.PARAMETER WorkerName
    The App Service name (matches the system-assigned managed identity display name).

.PARAMETER Permissions
    Array of Microsoft Graph application permission names to assign.
    Defaults to the full set required by IntuneAssistant Worker.

.EXAMPLE
    # Use defaults (all required permissions)
    .\grand-worker-permissions.ps1 -TenantId "6a80764a-..." -WorkerName "my-worker"

.EXAMPLE
    # Assign only specific permissions
    .\grand-worker-permissions.ps1 -TenantId "6a80764a-..." -WorkerName "my-worker" `
        -Permissions @('Mail.Send', 'Group.Read.All')

.NOTES
    Requires: Privileged Role Administrator or Global Administrator role in Entra ID.
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true, HelpMessage = "Your Azure AD Tenant ID")]
    [ValidateNotNullOrEmpty()]
    [string]$TenantId,

    [Parameter(Mandatory = $true, HelpMessage = "App Service name (= managed identity display name)")]
    [ValidateNotNullOrEmpty()]
    [string]$WorkerName,

    [Parameter(Mandatory = $false, HelpMessage = "Graph application permissions to assign")]
    [string[]]$Permissions = @(
        # Mail
        'Mail.Send',
        # Identity & directory
        'Group.Read.All',
        'GroupMember.Read.All',
        'User.ReadBasic.All',
        'RoleManagement.Read.Directory',
        'AuditLog.Read.All',
        'Application.Read.All',
        # Device management
        'DeviceManagementConfiguration.Read.All',
        'DeviceManagementScripts.Read.All',
        'DeviceManagementRBAC.Read.All',
        'DeviceManagementApps.Read.All',
        'DeviceManagementServiceConfig.Read.All',
        'DeviceManagementManagedDevices.Read.All',
        # Policy & monitoring
        'Policy.Read.ConditionalAccess',
        'ConfigurationMonitoring.ReadWrite.All'  # non-standard — skipped if not found
    )
)

$ErrorActionPreference = 'Stop'

# ─────────────────────────────────────────────────────────────────────────────
# CONSTANTS
# ─────────────────────────────────────────────────────────────────────────────
$MsGraphAppId = '00000003-0000-0000-c000-000000000000'  # Microsoft Graph
$GraphBaseUrl = 'https://graph.microsoft.com/v1.0'

# ─────────────────────────────────────────────────────────────────────────────
# ENSURE MODULE (authentication only — not the full Graph SDK)
# ─────────────────────────────────────────────────────────────────────────────
$moduleName = 'Microsoft.Graph.Authentication'
if (-not (Get-Module -ListAvailable -Name $moduleName)) {
    Write-Host "Module '$moduleName' not found. Installing..." -ForegroundColor Yellow
    Install-Module $moduleName -Scope CurrentUser -Force -AllowClobber
    Write-Host "  ✓ Installed" -ForegroundColor Green
}
Import-Module $moduleName -ErrorAction Stop

# ─────────────────────────────────────────────────────────────────────────────
# HELPERS
# ─────────────────────────────────────────────────────────────────────────────
function Invoke-Graph {
    param(
        [string]$Path,
        [string]$Method = 'GET',
        [hashtable]$Body
    )
    $params = @{
        Uri        = "$GraphBaseUrl$Path"
        Method     = $Method
        OutputType = 'PSObject'
    }
    if ($Body) { $params.Body = ($Body | ConvertTo-Json -Depth 5) }
    return Invoke-MgGraphRequest @params
}

function Get-GraphAllPages {
    param([string]$Path)
    $items = [System.Collections.Generic.List[object]]::new()
    $uri = "$GraphBaseUrl$Path"
    while ($uri) {
        $resp = Invoke-MgGraphRequest -Uri $uri -Method GET -OutputType PSObject
        if ($resp.value) { $items.AddRange([object[]]$resp.value) }
        $uri = $resp.'@odata.nextLink'
    }
    return $items
}

# ─────────────────────────────────────────────────────────────────────────────
# BANNER
# ─────────────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "  Worker Managed Identity — Graph Permission Setup" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "  TenantId   : $TenantId"
Write-Host "  WorkerName : $WorkerName"
Write-Host "  Permissions: $($Permissions.Count) requested"
Write-Host ""

# ─────────────────────────────────────────────────────────────────────────────
# STEP 1: INTERACTIVE LOGIN
# ─────────────────────────────────────────────────────────────────────────────
Write-Host "[1/5] Opening browser for interactive login..." -ForegroundColor Yellow
Write-Host "      (MFA and Conditional Access are fully supported)" -ForegroundColor Gray

Connect-MgGraph `
    -TenantId $TenantId `
    -Scopes 'Application.Read.All', 'AppRoleAssignment.ReadWrite.All' `
    -NoWelcome

$ctx = Get-MgContext
Write-Host "  ✓ Signed in as : $($ctx.Account)" -ForegroundColor Green
Write-Host "  ✓ Tenant       : $($ctx.TenantId)" -ForegroundColor Gray

# ─────────────────────────────────────────────────────────────────────────────
# STEP 2: FIND MANAGED IDENTITY SERVICE PRINCIPAL
# ─────────────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "[2/5] Searching for managed identity: '$WorkerName'..." -ForegroundColor Yellow

$encoded    = [uri]::EscapeDataString("displayName eq '$WorkerName'")
$spResp     = Invoke-Graph -Path "/servicePrincipals?`$filter=$encoded&`$select=id,displayName,appId,servicePrincipalType"
$allMatches = @($spResp.value)

if ($allMatches.Count -eq 0) {
    Write-Error @"

No service principal found with displayName '$WorkerName'.

Make sure:
  1. The App Service exists in this tenant
  2. System-assigned managed identity is ENABLED
     Azure Portal → App Service → Identity → System assigned → Status: On
  3. The name matches exactly (case-sensitive)
"@
    Disconnect-MgGraph | Out-Null
    exit 1
}

$managedIdentity = $allMatches |
    Where-Object { $_.servicePrincipalType -eq 'ManagedIdentity' } |
    Select-Object -First 1
if (-not $managedIdentity) { $managedIdentity = $allMatches[0] }

Write-Host "  ✓ Found: $($managedIdentity.displayName)" -ForegroundColor Green
Write-Host "    Object ID : $($managedIdentity.id)" -ForegroundColor Gray
Write-Host "    Type      : $($managedIdentity.servicePrincipalType)" -ForegroundColor Gray

if ($managedIdentity.servicePrincipalType -ne 'ManagedIdentity') {
    Write-Warning "Type is not 'ManagedIdentity' — verify system-assigned identity is enabled on the App Service."
}

# ─────────────────────────────────────────────────────────────────────────────
# STEP 3: FIND MICROSOFT GRAPH SP + REQUIRED APP ROLES
# ─────────────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "[3/5] Finding Microsoft Graph service principal and app roles..." -ForegroundColor Yellow

$graphFilter = [uri]::EscapeDataString("appId eq '$MsGraphAppId'")
$graphResp   = Invoke-Graph -Path "/servicePrincipals?`$filter=$graphFilter&`$select=id,displayName,appRoles"
$graphSp     = @($graphResp.value)[0]

if (-not $graphSp) {
    Write-Error "Microsoft Graph service principal not found in tenant '$TenantId'."
    Disconnect-MgGraph | Out-Null
    exit 1
}
Write-Host "  ✓ Found Microsoft Graph (Object ID: $($graphSp.id))" -ForegroundColor Green

$roleMap = [ordered]@{}
foreach ($permName in $Permissions) {
    $role = $graphSp.appRoles | Where-Object {
        $_.value -eq $permName -and $_.allowedMemberTypes -contains 'Application'
    }
    if ($role) {
        $roleMap[$permName] = $role.id
        Write-Host "  ✓ Located : $permName" -ForegroundColor Gray
    } else {
        Write-Warning "  Skipped  : '$permName' — not found as application permission in Microsoft Graph"
    }
}

if ($roleMap.Count -eq 0) {
    Write-Error "None of the requested permissions could be located in Microsoft Graph."
    Disconnect-MgGraph | Out-Null
    exit 1
}

# ─────────────────────────────────────────────────────────────────────────────
# STEP 4: CHECK EXISTING ASSIGNMENTS
# ─────────────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "[4/5] Checking existing assignments on managed identity..." -ForegroundColor Yellow

$existing = Get-GraphAllPages -Path "/servicePrincipals/$($managedIdentity.id)/appRoleAssignments"
Write-Host "  Found $($existing.Count) existing app role assignment(s)" -ForegroundColor Gray

# ─────────────────────────────────────────────────────────────────────────────
# STEP 5: ASSIGN MISSING PERMISSIONS
# ─────────────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "[5/5] Assigning missing permissions..." -ForegroundColor Yellow

$assigned = 0; $alreadyHad = 0; $failed = 0

foreach ($permName in $roleMap.Keys) {
    $roleId          = $roleMap[$permName]
    $alreadyAssigned = $existing | Where-Object {
        $_.resourceId -eq $graphSp.id -and $_.appRoleId -eq $roleId
    }

    if ($alreadyAssigned) {
        Write-Host "  ○ Already assigned : $permName" -ForegroundColor Cyan
        $alreadyHad++
        continue
    }

    try {
        Invoke-Graph `
            -Path "/servicePrincipals/$($managedIdentity.id)/appRoleAssignments" `
            -Method POST `
            -Body @{
                principalId = $managedIdentity.id
                resourceId  = $graphSp.id
                appRoleId   = $roleId
            } | Out-Null

        Write-Host "  ✓ Assigned         : $permName" -ForegroundColor Green
        $assigned++
    }
    catch {
        Write-Host "  ✗ Failed           : $permName — $($_.Exception.Message)" -ForegroundColor Red
        $failed++
    }
}

# ─────────────────────────────────────────────────────────────────────────────
# SUMMARY
# ─────────────────────────────────────────────────────────────────────────────
Disconnect-MgGraph | Out-Null

Write-Host ""
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "  Summary" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "  Worker         : $($managedIdentity.displayName)"
Write-Host "  Object ID      : $($managedIdentity.id)"
Write-Host "  Newly assigned : $assigned" -ForegroundColor $(if ($assigned -gt 0) { 'Green' } else { 'White' })
Write-Host "  Already had    : $alreadyHad"
Write-Host "  Failed         : $failed"   -ForegroundColor $(if ($failed -gt 0) { 'Red' } else { 'White' })

if ($failed -gt 0) {
    Write-Host ""
    Write-Host "    Failures may indicate insufficient role." -ForegroundColor Yellow
    Write-Host "    Required: Application Administrator or Global Administrator" -ForegroundColor Yellow
}

if ($assigned -gt 0 -or $alreadyHad -gt 0) {
    Write-Host ""
    Write-Host "    IMPORTANT — Exchange Online step still required for Mail.Send" -ForegroundColor Yellow
    Write-Host "    Connect-ExchangeOnline" -ForegroundColor White
    Write-Host "    Test-ApplicationAccessPolicy -Identity <sender@domain> -AppId $($managedIdentity.id)" -ForegroundColor White
}

Write-Host ""
Write-Host "  Done!" -ForegroundColor Green
Write-Host ""

exit $(if ($failed -gt 0) { 1 } else { 0 })
```

{% endstep %}

{% step %}

### Open a PowerShell window

Run the PowerShell file using the example below.

```
.\grand-worker-permissions.ps1 -TenantId "6a80764a-..." -WorkerName "my-worker"

```

{% endstep %}
{% endstepper %}

## Permissions for services that come with a module

A service such as Security Insights needs application permissions on top of the set the script above grants. They are granted to the same Worker identity. A part whose permission is missing is skipped and shown as such in the results; the rest still runs.

### Security Insights

| Resource                              | Permission                                                                                                                  | Needed for                                                                                                         |
| ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| Microsoft Graph                       | `Directory.Read.All`                                                                                                        | Licences, users, administrator roles and domains                                                                   |
| Microsoft Graph                       | `AuditLog.Read.All`                                                                                                         | MFA registration details                                                                                           |
| Microsoft Graph                       | `RoleManagement.Read.Directory`                                                                                             | Administrator roles, active and eligible                                                                           |
| Microsoft Graph                       | `Policy.Read.All`                                                                                                           | Tenant settings: authorization policy and Security Defaults                                                        |
| Microsoft Graph                       | `Policy.Read.ConditionalAccess`                                                                                             | Conditional Access policies                                                                                        |
| Microsoft Graph                       | `SecurityEvents.Read.All`                                                                                                   | Microsoft Secure Score                                                                                             |
| Microsoft Graph (optional)            | `Application.Read.All`                                                                                                      | Judging the user consent control (classification of app permissions)                                               |
| Microsoft Graph (optional)            | `OrganizationalBranding.Read.All`                                                                                           | The company branding control                                                                                       |
| Office 365 Exchange Online (optional) | `Exchange.ManageAsApp`, plus an Exchange administrator or security reader role on the Worker identity in Microsoft Entra ID | The ten Exchange and Defender for Office 365 controls                                                              |
| WindowsDefenderATP (optional)         | `Machine.Read.All`                                                                                                          | Device counters: onboarding, servers, inactive devices                                                             |
| WindowsDefenderATP (optional)         | `Vulnerability.Read.All`                                                                                                    | Vulnerability counters and missing updates. Without it the counters are left out and the data is marked incomplete |

The default script already grants `RoleManagement.Read.Directory`, `AuditLog.Read.All`, `Application.Read.All` and `Policy.Read.ConditionalAccess`. Pass the others to `-Permissions` for the Microsoft Graph part:

```powershell
.\grand-worker-permissions.ps1 -TenantId "<tenant id>" -WorkerName "<worker name>" `
    -Permissions @('Directory.Read.All','Policy.Read.All','SecurityEvents.Read.All','OrganizationalBranding.Read.All')
```

The script above only handles Microsoft Graph. To grant everything Security Insights needs in one run, including Exchange Online and Defender for Endpoint, use the script on the **Setup** page of Security Insights. It works the same way (it signs you in, finds the Worker by name, grants only what is missing, and can be run again):

```powershell
.\grant-module-permissions.ps1 -TenantId "<tenant id>" -WorkerName "<worker name>"
```

{% hint style="info" %}
Exchange Online also needs an Exchange administrator or security reader role assigned to the Worker's identity in Microsoft Entra ID (Roles and administrators). Defender for Endpoint must be active in the tenant for the device counters; without it that part is skipped.
{% endhint %}
