-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathGet-CIPPAlertExpiringLicenses.ps1
More file actions
74 lines (63 loc) · 3.04 KB
/
Get-CIPPAlertExpiringLicenses.ps1
File metadata and controls
74 lines (63 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function Get-CIPPAlertExpiringLicenses {
<#
.FUNCTIONALITY
Entrypoint
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[Alias('input')]
$InputValue,
$TenantFilter
)
try {
# Parse input parameters - default to 30 days if not specified
# Support both old format (direct value) and new format (object with properties)
if ($InputValue -is [hashtable] -or $InputValue -is [PSCustomObject]) {
$DaysThreshold = if ($InputValue.ExpiringLicensesDays) { [int]$InputValue.ExpiringLicensesDays } else { 30 }
$UnassignedOnly = if ($null -ne $InputValue.ExpiringLicensesUnassignedOnly) { [bool]$InputValue.ExpiringLicensesUnassignedOnly } else { $false }
} else {
# Backward compatibility: if InputValue is a simple value, treat it as days threshold
$DaysThreshold = if ($InputValue) { [int]$InputValue } else { 30 }
$UnassignedOnly = $false
}
$AlertData = @(
Get-CIPPLicenseOverview -TenantFilter $TenantFilter | ForEach-Object {
$UnassignedCount = [int]$_.CountAvailable
# If unassigned only filter is enabled, skip licenses with no unassigned units
if ($UnassignedOnly -and $UnassignedCount -le 0) {
return
}
# FIX: term rows are in TermInfo on the overview object
$TermData = @($_.TermInfo)
foreach ($Term in $TermData) {
$DaysUntilRenew = [int]$Term.DaysUntilRenew
if ($DaysUntilRenew -lt $DaysThreshold -and $DaysUntilRenew -gt 0) {
$Message = if ($UnassignedOnly) {
"$($_.License) has $UnassignedCount unassigned license(s) expiring in $DaysUntilRenew days. The estimated term is $($Term.Term)"
} else {
"$($_.License) will expire in $DaysUntilRenew days. The estimated term is $($Term.Term)"
}
[PSCustomObject]@{
Message = $Message
License = $_.License
SkuId = $_.skuId
DaysUntilRenew = $DaysUntilRenew
Term = $Term.Term
Status = $Term.Status
TotalLicenses = $Term.TotalLicenses
CountUsed = $_.CountUsed
CountAvailable = $UnassignedCount
NextLifecycle = $Term.NextLifecycle
Tenant = $_.Tenant
}
}
}
}
)
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $AlertData
} catch {
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -error $_
throw
}
}