Get Compliance Policy Coverage

This script compares the platforms of all enrolled Intune devices against the platforms targeted by assigned compliance policies. Platforms with enrolled devices but no assigned compliance policy are a real gap: those devices report as compliant by default (or fall to the built-in policy) and can slip through Conditional Access checks. The report also lists compliance policies that exist but are not assigned to anything, and the device count per platform so gaps can be prioritized.

SecurityCompliance
606 views192 downloadsVersion 1.0By Ugur Koc
View on GitHub

New to runbook deployment? Follow the step-by-step guide from the Deploy to Azure button to the first scheduled run, including granting Graph permissions to the managed identity.

// QUALITY CHECKS

Validation status

Quality checks

All checks pass
  • ParsePass
  • LintPass
  • MetadataPass
  • Runbook-readyPass
  • Module depsPass

Tests run automatically on every change. What does each check mean?

// REQUIRED PERMISSIONS

Microsoft Graph scopes

DeviceManagementConfiguration.Read.All

Allows the app to read properties of Microsoft Intune-managed device configuration and device compliance policies and their assignment to groups, without a signed-in user.

DeviceManagementManagedDevices.Read.All

Allows the app to read the properties of devices managed by Microsoft Intune, without a signed-in user.

Running this as an Azure Automation runbook? These scopes must be granted to the account's managed identity, which has no portal UI. The deployment walkthrough shows the exact Cloud Shell commands.

// CHANGELOG

Version history

  1. Entry · 01

    1.0 - Initial release

// CODE

Source

get-compliance-policy-coverage.ps1
<#
.TITLE
    Get Compliance Policy Coverage

.SYNOPSIS
    Finds device platforms in the tenant that no assigned compliance policy covers.

.DESCRIPTION
    This script compares the platforms of all enrolled Intune devices against the
    platforms targeted by assigned compliance policies. Platforms with enrolled
    devices but no assigned compliance policy are a real gap: those devices report
    as compliant by default (or fall to the built-in policy) and can slip through
    Conditional Access checks. The report also lists compliance policies that exist
    but are not assigned to anything, and the device count per platform so gaps can
    be prioritized.

.TAGS
    Security,Compliance

.MINROLE
    Intune Administrator

.PERMISSIONS
    DeviceManagementConfiguration.Read.All,DeviceManagementManagedDevices.Read.All

.AUTHOR
    Ugur Koc

.VERSION
    1.0

.CHANGELOG
    1.0 - Initial release

.LASTUPDATE
    2026-07-20

.EXAMPLE
    .\get-compliance-policy-coverage.ps1
    Shows the platform coverage matrix and any gaps

.EXAMPLE
    .\get-compliance-policy-coverage.ps1 -ExportToCsv
    Exports the coverage report to a timestamped CSV file

.NOTES
    - Requires Microsoft.Graph.Authentication module
    - Policy platform is derived from the policy's OData type (windows10CompliancePolicy targets Windows, etc.)
    - A platform counts as covered when at least one policy for it has at least one assignment; group scoping within the platform is not evaluated
    - Check the tenant's "Mark devices with no compliance policy assigned as" setting: if it is set to Compliant, uncovered platforms silently pass Conditional Access
    - Uses beta Graph endpoints for consistency with the rest of the library
    - Local interactive sign-in uses the MgGraphCommunity module to avoid the Graph SDK's mandatory WAM broker on Windows
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory = $false, HelpMessage = "Export results to CSV")]
    [switch]$ExportToCsv,

    [Parameter(Mandatory = $false, HelpMessage = "Output path for exports")]
    [string]$OutputPath = ".",

    [Parameter(Mandatory = $false, HelpMessage = "Force module installation without prompting")]
    [switch]$ForceModuleInstall
)

# ============================================================================
# ENVIRONMENT DETECTION AND SETUP
# ============================================================================

function Initialize-RequiredModule {
    param(
        [string[]]$ModuleNames,
        [bool]$IsAutomationEnvironment,
        [bool]$ForceInstall = $false
    )

    foreach ($ModuleName in $ModuleNames) {
        Write-Verbose "Checking module: $ModuleName"

        $module = Get-Module -ListAvailable -Name $ModuleName | Select-Object -First 1

        if (-not $module) {
            if ($IsAutomationEnvironment) {
                throw "Module '$ModuleName' is not available in Azure Automation"
            }
            else {
                Write-Information "Module '$ModuleName' not found. Installing..." -InformationAction Continue

                if (-not $ForceInstall) {
                    $response = Read-Host "Install module '$ModuleName'? (Y/N)"
                    if ($response -notmatch '^[Yy]') {
                        throw "Module '$ModuleName' is required but installation was declined."
                    }
                }

                try {
                    $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
                    $scope = if ($isAdmin) { "AllUsers" } else { "CurrentUser" }

                    Install-Module -Name $ModuleName -Scope $scope -Force -AllowClobber -Repository PSGallery
                    Write-Information "✓ Successfully installed '$ModuleName'" -InformationAction Continue
                }
                catch {
                    throw "Failed to install module '$ModuleName': $($_.Exception.Message)"
                }
            }
        }

        Import-Module -Name $ModuleName -Force -ErrorAction Stop
    }
}

# Detect execution environment
$IsAzureAutomation = $null -ne $PSPrivateMetadata.JobId.Guid

# Initialize required modules
$RequiredModules = @("Microsoft.Graph.Authentication")

# MgGraphCommunity gives WAM-free interactive sign-in for local runs
if (-not $IsAzureAutomation) {
    $RequiredModules += "MgGraphCommunity"
}

try {
    Initialize-RequiredModule -ModuleNames $RequiredModules -IsAutomationEnvironment $IsAzureAutomation -ForceInstall $ForceModuleInstall
    Write-Verbose "✓ All required modules are available"
}
catch {
    Write-Error "Module initialization failed: $_"
    exit 1
}

# ============================================================================
# AUTHENTICATION
# ============================================================================

try {
    if ($IsAzureAutomation) {
        Write-Output "Connecting to Microsoft Graph using Managed Identity..."
        Connect-MgGraph -Identity -NoWelcome -ErrorAction Stop
    }
    else {
        Write-Information "Connecting to Microsoft Graph..." -InformationAction Continue
        $Scopes = @(
            "DeviceManagementConfiguration.Read.All",
            "DeviceManagementManagedDevices.Read.All"
        )
        Connect-MgGraphCommunity -Scopes $Scopes -NoWelcome -ErrorAction Stop
    }
    Write-Information "✓ Successfully connected to Microsoft Graph" -InformationAction Continue
}
catch {
    Write-Error "Failed to connect to Microsoft Graph: $($_.Exception.Message)"
    exit 1
}

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

function Get-MgGraphAllPage {
    param(
        [string]$Uri,
        [int]$DelayMs = 100
    )

    $allResults = @()
    $nextLink = $Uri

    do {
        try {
            if ($allResults.Count -gt 0) {
                Start-Sleep -Milliseconds $DelayMs
            }

            $response = Invoke-MgGraphRequest -Uri $nextLink -Method GET

            if ($response.value) {
                $allResults += $response.value
            }
            else {
                $allResults += $response
            }

            $nextLink = $response.'@odata.nextLink'
        }
        catch {
            if ($_.Exception.Message -like "*429*") {
                Write-Information "Rate limit hit, waiting 60 seconds..." -InformationAction Continue
                Start-Sleep -Seconds 60
                continue
            }
            Write-Warning "Error fetching data: $($_.Exception.Message)"
            break
        }
    } while ($nextLink)

    return $allResults
}

function Get-PolicyPlatform {
    param([string]$ODataType)

    # Compliance policy types map 1:1 to platforms
    switch -Wildcard ($ODataType) {
        "*windows10CompliancePolicy" { "Windows" }
        "*windows81CompliancePolicy" { "Windows" }
        "*macOSCompliancePolicy" { "macOS" }
        "*iosCompliancePolicy" { "iOS/iPadOS" }
        "*androidCompliancePolicy" { "Android" }
        "*androidWorkProfileCompliancePolicy" { "Android" }
        "*androidDeviceOwnerCompliancePolicy" { "Android" }
        "*aospDeviceOwnerCompliancePolicy" { "Android" }
        "*linuxCompliancePolicy" { "Linux" }
        default { ($ODataType -replace "#microsoft.graph.", "") -replace "CompliancePolicy", "" }
    }
}

function Get-DevicePlatform {
    param([string]$OperatingSystem)

    switch -Wildcard ($OperatingSystem) {
        "Windows*" { "Windows" }
        "macOS*" { "macOS" }
        "iOS*" { "iOS/iPadOS" }
        "iPadOS*" { "iOS/iPadOS" }
        "Android*" { "Android" }
        "Linux*" { "Linux" }
        default { $OperatingSystem }
    }
}

# ============================================================================
# MAIN SCRIPT LOGIC
# ============================================================================

try {
    Write-Information "Retrieving compliance policies with assignments..." -InformationAction Continue
    $policies = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies?`$expand=assignments"
    Write-Information "✓ Found $(@($policies).Count) compliance policies" -InformationAction Continue

    Write-Information "Retrieving managed devices..." -InformationAction Continue
    $devices = Get-MgGraphAllPage -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$select=id,operatingSystem,complianceState"
    Write-Information "✓ Found $(@($devices).Count) managed devices" -InformationAction Continue

    # Device counts per platform
    $devicePlatforms = @{}
    foreach ($device in $devices) {
        $platform = Get-DevicePlatform -OperatingSystem ([string]$device.operatingSystem)
        if (-not $devicePlatforms.ContainsKey($platform)) { $devicePlatforms[$platform] = 0 }
        $devicePlatforms[$platform]++
    }

    # Assigned policy counts per platform
    $platformPolicies = @{}
    [System.Collections.Generic.List[Object]]$unassignedPolicies = @()

    foreach ($policy in $policies) {
        $platform = Get-PolicyPlatform -ODataType ([string]$policy.'@odata.type')
        $isAssigned = (@($policy.assignments).Count -gt 0)

        if ($isAssigned) {
            if (-not $platformPolicies.ContainsKey($platform)) {
                $platformPolicies[$platform] = [System.Collections.Generic.List[string]]::new()
            }
            $platformPolicies[$platform].Add($policy.displayName)
        }
        else {
            $unassignedPolicies.Add([PSCustomObject]@{
                    PolicyName = $policy.displayName
                    Platform   = $platform
                })
        }
    }

    # Build coverage matrix
    [System.Collections.Generic.List[Object]]$report = @()
    foreach ($platform in ($devicePlatforms.Keys | Sort-Object)) {
        $assignedPolicyNames = if ($platformPolicies.ContainsKey($platform)) { @($platformPolicies[$platform]) } else { @() }
        $noncompliantCount = @($devices | Where-Object { (Get-DevicePlatform -OperatingSystem ([string]$_.operatingSystem)) -eq $platform -and $_.complianceState -eq "noncompliant" }).Count

        $report.Add([PSCustomObject]@{
                Platform           = $platform
                DeviceCount        = $devicePlatforms[$platform]
                NoncompliantCount  = $noncompliantCount
                AssignedPolicies   = $assignedPolicyNames.Count
                PolicyNames        = ($assignedPolicyNames -join "; ")
                CoverageStatus     = if ($assignedPolicyNames.Count -gt 0) { "Covered" } else { "NOT COVERED" }
            })
    }

    # ----- Display results -----
    Write-Information "`nCOMPLIANCE POLICY COVERAGE" -InformationAction Continue
    Write-Information ("=" * 50) -InformationAction Continue
    Write-Information "Generated: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -InformationAction Continue
    Write-Information ("=" * 50) -InformationAction Continue

    foreach ($row in $report) {
        Write-Information "`n[$($row.CoverageStatus)] $($row.Platform)" -InformationAction Continue
        Write-Information "  Devices: $($row.DeviceCount) ($($row.NoncompliantCount) noncompliant)" -InformationAction Continue
        if ($row.AssignedPolicies -gt 0) {
            Write-Information "  Assigned policies: $($row.PolicyNames)" -InformationAction Continue
        }
        else {
            Write-Information "  No assigned compliance policy targets this platform" -InformationAction Continue
        }
    }

    if ($unassignedPolicies.Count -gt 0) {
        Write-Information "`nUnassigned compliance policies:" -InformationAction Continue
        foreach ($row in ($unassignedPolicies | Sort-Object Platform, PolicyName)) {
            Write-Information "  $($row.PolicyName) [$($row.Platform)]" -InformationAction Continue
        }
    }

    # Summary
    $gapPlatforms = @($report | Where-Object { $_.CoverageStatus -eq "NOT COVERED" })
    Write-Information "`n" -InformationAction Continue
    Write-Information ("=" * 50) -InformationAction Continue
    Write-Information "Summary: $($report.Count) platforms with devices, $($gapPlatforms.Count) without compliance coverage, $($unassignedPolicies.Count) unassigned policies" -InformationAction Continue
    if ($gapPlatforms.Count -gt 0) {
        Write-Information "Gap platforms: $(($gapPlatforms | ForEach-Object { $_.Platform }) -join ', ')" -InformationAction Continue
    }
    Write-Information ("=" * 50) -InformationAction Continue

    # Export to CSV if requested
    if ($ExportToCsv) {
        $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
        $csvPath = Join-Path $OutputPath "Compliance_Coverage_$timestamp.csv"
        $report | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
        Write-Information "✓ CSV report saved: $csvPath" -InformationAction Continue
    }
}
catch {
    Write-Error "Script execution failed: $($_.Exception.Message)"
    exit 1
}
finally {
    try {
        $null = Disconnect-MgGraph
        Write-Information "✓ Disconnected from Microsoft Graph" -InformationAction Continue
    }
    catch {
        Write-Verbose "Graph disconnection completed"
    }
}

// NOTES

Author notes

- Requires Microsoft.Graph.Authentication module - Policy platform is derived from the policy's OData type (windows10CompliancePolicy targets Windows, etc.) - A platform counts as covered when at least one policy for it has at least one assignment; group scoping within the platform is not evaluated - Check the tenant's "Mark devices with no compliance policy assigned as" setting: if it is set to Compliant, uncovered platforms silently pass Conditional Access - Uses beta Graph endpoints for consistency with the rest of the library - Local interactive sign-in uses the MgGraphCommunity module to avoid the Graph SDK's mandatory WAM broker on Windows

// RELATED

Picked by shared tags, category, and script type — nothing magic, just metadata overlap.

  1. Multi-Admin Approval Compliance Dashboard Report

    This script connects to Microsoft Graph and analyzes Multi-Admin Approval configurations, usage patterns, and compliance metrics across your Intune environment. It generates detailed reports showing MAA coverage gaps, approval statistics, admin permissions, and trends. The script helps organizations ensure proper implementation of MAA controls and identify areas for security improvement. Reports are generated in both HTML and CSV formats for different audiences.

    ComplianceSecurity
  2. Multi-Admin Approval Pending Requests Monitor

    This script is designed to run as a scheduled Azure Automation runbook that monitors Multi-Admin Approval requests in Microsoft Intune and identifies pending approval requests. It tracks new requests, monitors request age, identifies approvers, and sends email notifications to administrators with detailed request information and direct links to the Intune portal. The script helps maintain security compliance by ensuring timely review of administrative changes and provides visibility into the MAA approval workflow. Key Features: - Monitors all MAA pending requests across protected resources - Tracks request age and highlights urgent requests - Identifies and notifies appropriate approvers - Provides direct links to Intune portal for quick action - Tracks previously notified requests to avoid spam - Sends escalation alerts for aging requests - Supports both Azure Automation runbook and local execution - HTML formatted email reports with actionable insights - Uses Microsoft Graph Mail API exclusively

    SecurityCompliance
  3. BitLocker Keys Backup to Azure Key Vault

    This script connects to Microsoft Graph API to retrieve BitLocker recovery keys for Windows devices, then stores them securely in Azure Key Vault using REST API. Each key is stored as a secret with device information (name and serial number) included in tags. Authentication uses the MgGraphCommunity module (WAM-free) and acquires two separate tokens with the correct audiences: a device code sign-in for Azure Key Vault (https://vault.azure.net) and an interactive browser sign-in for Microsoft Graph. No Az modules are needed. On first run, you will be prompted to consent to the required permissions including Key Vault access.

    SecurityCompliance