|
| 1 | +function Compare-CIPPIntuneAssignments { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Compares existing Intune policy assignments against expected assignment settings. |
| 5 | + .DESCRIPTION |
| 6 | + Returns $true if the existing assignments match the expected settings, $false if they differ, |
| 7 | + or $null if the comparison could not be completed (e.g. Graph error). |
| 8 | + .PARAMETER ExistingAssignments |
| 9 | + The current assignments on the policy, as returned by Get-CIPPIntunePolicyAssignments. |
| 10 | + .PARAMETER ExpectedAssignTo |
| 11 | + The expected assignment target type: allLicensedUsers, AllDevices, AllDevicesAndUsers, |
| 12 | + customGroup, or On (no assignment). |
| 13 | + .PARAMETER ExpectedCustomGroup |
| 14 | + The expected custom group name(s), comma-separated. Used when ExpectedAssignTo is 'customGroup'. |
| 15 | + .PARAMETER ExpectedExcludeGroup |
| 16 | + The expected exclusion group name(s), comma-separated. |
| 17 | + .PARAMETER ExpectedAssignmentFilter |
| 18 | + The expected assignment filter display name. Wildcards supported. |
| 19 | + .PARAMETER ExpectedAssignmentFilterType |
| 20 | + 'include' or 'exclude'. Defaults to 'include'. |
| 21 | + .PARAMETER TenantFilter |
| 22 | + The tenant to query for group/filter resolution. |
| 23 | + .FUNCTIONALITY |
| 24 | + Internal |
| 25 | + #> |
| 26 | + param( |
| 27 | + [object[]]$ExistingAssignments, |
| 28 | + [string]$ExpectedAssignTo, |
| 29 | + [string]$ExpectedCustomGroup, |
| 30 | + [string]$ExpectedExcludeGroup, |
| 31 | + [string]$ExpectedAssignmentFilter, |
| 32 | + [string]$ExpectedAssignmentFilterType = 'include', |
| 33 | + [Parameter(Mandatory = $true)] |
| 34 | + [string]$TenantFilter |
| 35 | + ) |
| 36 | + |
| 37 | + try { |
| 38 | + # Normalize existing targets |
| 39 | + $ExistingTargetTypes = @($ExistingAssignments.target.'@odata.type' | Where-Object { $_ }) |
| 40 | + $ExistingIncludeGroupIds = @( |
| 41 | + $ExistingAssignments | |
| 42 | + Where-Object { $_.target.'@odata.type' -eq '#microsoft.graph.groupAssignmentTarget' } | |
| 43 | + ForEach-Object { $_.target.groupId } |
| 44 | + ) |
| 45 | + $ExistingExcludeGroupIds = @( |
| 46 | + $ExistingAssignments | |
| 47 | + Where-Object { $_.target.'@odata.type' -eq '#microsoft.graph.exclusionGroupAssignmentTarget' } | |
| 48 | + ForEach-Object { $_.target.groupId } |
| 49 | + ) |
| 50 | + |
| 51 | + # Determine expected include target types |
| 52 | + $ExpectedIncludeTypes = switch ($ExpectedAssignTo) { |
| 53 | + 'allLicensedUsers' { @('#microsoft.graph.allLicensedUsersAssignmentTarget') } |
| 54 | + 'AllDevices' { @('#microsoft.graph.allDevicesAssignmentTarget') } |
| 55 | + 'AllDevicesAndUsers' { @('#microsoft.graph.allDevicesAssignmentTarget', '#microsoft.graph.allLicensedUsersAssignmentTarget') } |
| 56 | + 'customGroup' { @('#microsoft.graph.groupAssignmentTarget') } |
| 57 | + 'On' { @() } |
| 58 | + default { @() } |
| 59 | + } |
| 60 | + |
| 61 | + # Compare include target types (ignore exclusion targets) |
| 62 | + $ExistingIncludeTypes = @($ExistingTargetTypes | Where-Object { $_ -ne '#microsoft.graph.exclusionGroupAssignmentTarget' }) |
| 63 | + $TargetTypeMatch = $true |
| 64 | + foreach ($t in $ExpectedIncludeTypes) { |
| 65 | + if ($t -notin $ExistingIncludeTypes) { $TargetTypeMatch = $false; break } |
| 66 | + } |
| 67 | + if ($TargetTypeMatch) { |
| 68 | + foreach ($t in $ExistingIncludeTypes) { |
| 69 | + if ($t -notin $ExpectedIncludeTypes) { $TargetTypeMatch = $false; break } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + # Lazy-load groups cache only if needed |
| 74 | + $AllGroupsCache = $null |
| 75 | + |
| 76 | + # For custom groups, resolve names to IDs and compare |
| 77 | + $IncludeGroupMatch = $true |
| 78 | + if ($ExpectedAssignTo -eq 'customGroup' -and $ExpectedCustomGroup) { |
| 79 | + $AllGroupsCache = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/groups?$select=id,displayName&$top=999' -tenantid $TenantFilter |
| 80 | + $ExpectedGroupIds = @( |
| 81 | + $ExpectedCustomGroup.Split(',').Trim() | ForEach-Object { |
| 82 | + $name = $_ |
| 83 | + $AllGroupsCache | Where-Object { $_.displayName -like $name } | Select-Object -ExpandProperty id |
| 84 | + } | Where-Object { $_ } |
| 85 | + ) |
| 86 | + $MissingIds = @($ExpectedGroupIds | Where-Object { $_ -notin $ExistingIncludeGroupIds }) |
| 87 | + $ExtraIds = @($ExistingIncludeGroupIds | Where-Object { $_ -notin $ExpectedGroupIds }) |
| 88 | + $IncludeGroupMatch = ($MissingIds.Count -eq 0 -and $ExtraIds.Count -eq 0) |
| 89 | + } |
| 90 | + |
| 91 | + # Compare exclusion groups |
| 92 | + $ExcludeGroupMatch = $true |
| 93 | + if ($ExpectedExcludeGroup) { |
| 94 | + if (-not $AllGroupsCache) { |
| 95 | + $AllGroupsCache = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/groups?$select=id,displayName&$top=999' -tenantid $TenantFilter |
| 96 | + } |
| 97 | + $ExpectedExcludeIds = @( |
| 98 | + $ExpectedExcludeGroup.Split(',').Trim() | ForEach-Object { |
| 99 | + $name = $_ |
| 100 | + $AllGroupsCache | Where-Object { $_.displayName -like $name } | Select-Object -ExpandProperty id |
| 101 | + } | Where-Object { $_ } |
| 102 | + ) |
| 103 | + $MissingExcludeIds = @($ExpectedExcludeIds | Where-Object { $_ -notin $ExistingExcludeGroupIds }) |
| 104 | + $ExtraExcludeIds = @($ExistingExcludeGroupIds | Where-Object { $_ -notin $ExpectedExcludeIds }) |
| 105 | + $ExcludeGroupMatch = ($MissingExcludeIds.Count -eq 0 -and $ExtraExcludeIds.Count -eq 0) |
| 106 | + } elseif ($ExistingExcludeGroupIds.Count -gt 0) { |
| 107 | + # No exclusions expected but some exist |
| 108 | + $ExcludeGroupMatch = $false |
| 109 | + } |
| 110 | + |
| 111 | + # Compare assignment filter |
| 112 | + $FilterMatch = $true |
| 113 | + if ($ExpectedAssignmentFilter) { |
| 114 | + $ExistingFilterIds = @( |
| 115 | + $ExistingAssignments | |
| 116 | + Where-Object { $_.target.deviceAndAppManagementAssignmentFilterId } | |
| 117 | + ForEach-Object { $_.target.deviceAndAppManagementAssignmentFilterId } |
| 118 | + ) |
| 119 | + if ($ExistingFilterIds.Count -eq 0) { |
| 120 | + $FilterMatch = $false |
| 121 | + } else { |
| 122 | + $AllFilters = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/assignmentFilters' -tenantid $TenantFilter |
| 123 | + $ExpectedFilter = $AllFilters | Where-Object { $_.displayName -like $ExpectedAssignmentFilter } | Select-Object -First 1 |
| 124 | + $FilterMatch = $ExpectedFilter -and ($ExpectedFilter.id -in $ExistingFilterIds) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return $TargetTypeMatch -and $IncludeGroupMatch -and $ExcludeGroupMatch -and $FilterMatch |
| 129 | + |
| 130 | + } catch { |
| 131 | + Write-Warning "Compare-CIPPIntuneAssignments failed for tenant $TenantFilter : $($_.Exception.Message)" |
| 132 | + return $null # null = unknown, don't treat as mismatch |
| 133 | + } |
| 134 | +} |
0 commit comments