Skip to content

Commit 15efd3d

Browse files
committed
feat(alert): include license names, SKUs and account status in inactive licensed users alert
The alert already fetched assignedLicenses and accountEnabled from Graph but dropped them from the emitted alert data. Surface license display names, raw SKU IDs, account enablement and days since last sign-in so the alert can be actioned without a follow-up lookup.
1 parent 951f831 commit 15efd3d

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

Modules/CIPPAlerts/Public/Alerts/Get-CIPPAlertInactiveLicensedUsers.ps1

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ function Get-CIPPAlertInactiveLicensedUsers {
4747
$GraphRequest = New-GraphGetRequest -uri $Uri -scope 'https://graph.microsoft.com/.default' -tenantid $TenantFilter |
4848
Where-Object { $null -ne $_.assignedLicenses -and $_.assignedLicenses.Count -gt 0 }
4949

50+
$LicenseOverview = @()
51+
try {
52+
$LicenseOverview = @(New-CIPPDbRequest -TenantFilter $TenantFilter -Type 'LicenseOverview')
53+
} catch {
54+
Write-Information "Could not get the license overview from the reporting DB, license names will fall back to SKU IDs: $($_.Exception.Message)"
55+
}
56+
5057
$AlertData = foreach ($user in $GraphRequest) {
5158
$lastInteractive = $user.signInActivity.lastSignInDateTime
5259
$lastNonInteractive = $user.signInActivity.lastNonInteractiveSignInDateTime
@@ -67,19 +74,28 @@ function Get-CIPPAlertInactiveLicensedUsers {
6774
if (-not $IncludeNeverSignedIn -and -not $lastSignIn) { continue }
6875
# Only process inactive users
6976
if ($isInactive) {
77+
$daysSinceSignIn = $null
7078
if (-not $lastSignIn) {
7179
$Message = 'User {0} has never signed in but still has a license assigned.' -f $user.UserPrincipalName
7280
} else {
7381
$daysSinceSignIn = [Math]::Round(((Get-Date) - [DateTime]$lastSignIn).TotalDays)
7482
$Message = 'User {0} has been inactive for {1} days but still has a license assigned. Last sign-in: {2}' -f $user.UserPrincipalName, $daysSinceSignIn, $lastSignIn
7583
}
84+
$LicenseNames = foreach ($SkuId in $user.assignedLicenses.skuId) {
85+
$Sku = $LicenseOverview | Where-Object { $_.skuId -eq $SkuId } | Select-Object -First 1
86+
if ($Sku.License) { $Sku.License } else { $SkuId }
87+
}
7688

7789
[PSCustomObject]@{
78-
UserPrincipalName = $user.UserPrincipalName
79-
Id = $user.id
80-
lastSignIn = $lastSignIn
81-
Message = $Message
82-
Tenant = $TenantFilter
90+
UserPrincipalName = $user.UserPrincipalName
91+
Id = $user.id
92+
lastSignIn = $lastSignIn
93+
DaysSinceLastSignIn = if ($null -ne $daysSinceSignIn) { $daysSinceSignIn } else { 'N/A' }
94+
AccountEnabled = $user.accountEnabled
95+
LicenseNames = $LicenseNames -join ', '
96+
SkuIds = @($user.assignedLicenses.skuId) -join ', '
97+
Message = $Message
98+
Tenant = $TenantFilter
8399
}
84100
}
85101
}

0 commit comments

Comments
 (0)