@@ -10,6 +10,28 @@ function Remove-CIPPLicense {
1010 )
1111
1212 if ($Schedule.IsPresent ) {
13+ # Record which licenses the user holds right now, so the offboarding result (and any
14+ # ticket/webhook/email it feeds) documents what was removed - useful for auditing and
15+ # for restoring the account to its previous state. Sku names come from the cached
16+ # license overview, which resolves the best available display name for the tenant.
17+ $LicenseSuffix = ' '
18+ try {
19+ $LicenseOverview = @ (New-CIPPDbRequest - TenantFilter $TenantFilter - Type ' LicenseOverview' )
20+ $UserLicenseState = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/users/$ ( $userid ) ?`$ select=licenseAssignmentStates" - tenantid $TenantFilter
21+ $ActiveSkuIds = @ (($UserLicenseState.licenseAssignmentStates | Where-Object { $_.state -eq ' Active' }).skuId | Select-Object - Unique)
22+ $LicenseNames = foreach ($SkuId in $ActiveSkuIds ) {
23+ $Sku = $LicenseOverview | Where-Object { $_.skuId -eq $SkuId } | Select-Object - First 1
24+ if ($Sku.License ) { $Sku.License } else { $SkuId }
25+ }
26+ if ($LicenseNames ) {
27+ $LicenseSuffix = " Licenses currently assigned that will be removed: $ ( @ ($LicenseNames | Sort-Object - Unique) -join ' , ' ) ."
28+ } else {
29+ $LicenseSuffix = ' The user currently has no licenses assigned.'
30+ }
31+ } catch {
32+ Write-Information " Could not enumerate current licenses for the scheduled removal message: $ ( $_.Exception.Message ) "
33+ }
34+
1335 $ScheduledTask = @ {
1436 TenantFilter = $TenantFilter
1537 Name = " Remove License: $Username "
@@ -30,7 +52,7 @@ function Remove-CIPPLicense {
3052 }
3153 }
3254 Add-CIPPScheduledTask - Task $ScheduledTask - hidden $false - DisallowDuplicateName $true
33- return " Scheduled license removal for $username "
55+ return " Scheduled license removal for $username . $LicenseSuffix "
3456 } else {
3557 try {
3658 $ConvertTable = [System.IO.File ]::ReadAllText((Join-Path $env: CIPPRootPath ' Config\ConversionTable.csv' )) | ConvertFrom-Csv
0 commit comments