Skip to content

Commit fe6bee8

Browse files
Merge branch 'dev' of https://github.com/KelvinTegelaar/CIPP-API into dev
2 parents baa663e + 7dd5581 commit fe6bee8

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function Get-CIPPAlertLicenseAssignmentErrors {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
#>
6+
[CmdletBinding()]
7+
Param (
8+
[Parameter(Mandatory)]
9+
$TenantFilter,
10+
[Alias('input')]
11+
$InputValue
12+
)
13+
14+
# Define error code translations for human-readable messages
15+
$ErrorTranslations = @(
16+
@{
17+
ErrorCode = "CountViolation"
18+
Description = "Not enough licenses available - the organization has exceeded the number of available licenses for this SKU"
19+
},
20+
@{
21+
ErrorCode = "MutuallyExclusiveViolation"
22+
Description = "Conflicting licenses assigned - this license cannot be assigned alongside another license the user already has"
23+
},
24+
@{
25+
ErrorCode = "ProhibitedInUsageLocationViolation"
26+
Description = "License not available in user's location - this license cannot be assigned to users in the user's current usage location"
27+
},
28+
@{
29+
ErrorCode = "UniquenessViolation"
30+
Description = "Duplicate license assignment - this license can only be assigned once per user"
31+
},
32+
@{
33+
ErrorCode = "Unknown"
34+
Description = "Unknown license assignment error - an unspecified error occurred during license assignment"
35+
}
36+
)
37+
38+
try {
39+
# Get all users with license assignment states from Graph API
40+
$Users = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$select=id,userPrincipalName,displayName,licenseAssignmentStates&`$top=999" -tenantid $TenantFilter
41+
42+
# Filter users who have license assignment violations
43+
$UsersWithViolations = $Users | Where-Object {
44+
$_.licenseAssignmentStates -and
45+
($_.licenseAssignmentStates | Where-Object {
46+
$_.error -and (
47+
$_.error -like "*CountViolation*" -or
48+
$_.error -like "*MutuallyExclusiveViolation*" -or
49+
$_.error -like "*ProhibitedInUsageLocationViolation*" -or
50+
$_.error -like "*UniquenessViolation*" -or
51+
$_.error -like "*Unknown*"
52+
)
53+
})
54+
}
55+
56+
# Build alert messages for users with violations
57+
$LicenseAssignmentErrors = foreach ($User in $UsersWithViolations) {
58+
$ViolationErrors = $User.licenseAssignmentStates | Where-Object {
59+
$_.error -and (
60+
$_.error -like "*CountViolation*" -or
61+
$_.error -like "*MutuallyExclusiveViolation*" -or
62+
$_.error -like "*ProhibitedInUsageLocationViolation*" -or
63+
$_.error -like "*UniquenessViolation*" -or
64+
$_.error -like "*Unknown*"
65+
)
66+
}
67+
68+
foreach ($Violation in $ViolationErrors) {
69+
# Find matching error translation
70+
$ErrorTranslation = $ErrorTranslations | Where-Object { $Violation.error -like "*$($_.ErrorCode)*" } | Select-Object -First 1
71+
$HumanReadableError = if ($ErrorTranslation) {
72+
$ErrorTranslation.Description
73+
} else {
74+
"Unknown license assignment error: $($Violation.error)"
75+
}
76+
77+
$PrettyName = Convert-SKUname -skuID $Violation.skuId
78+
79+
"$($User.userPrincipalName): $HumanReadableError (License: $PrettyName)"
80+
}
81+
}
82+
83+
# If errors are found, write alert
84+
if ($LicenseAssignmentErrors) {
85+
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $LicenseAssignmentErrors
86+
}
87+
88+
} catch {
89+
Write-LogMessage -message "Failed to check license assignment errors: $($_.exception.message)" -API 'License Assignment Alerts' -tenant $TenantFilter -sev Error
90+
}
91+
}

0 commit comments

Comments
 (0)