-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathGet-CIPPLicenseOverview.ps1
More file actions
162 lines (149 loc) · 7.91 KB
/
Copy pathGet-CIPPLicenseOverview.ps1
File metadata and controls
162 lines (149 loc) · 7.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function Get-CIPPLicenseOverview {
[CmdletBinding()]
param (
$TenantFilter,
$APIName = 'Get License Overview',
$Headers
)
$Requests = @(
@{
id = 'subscribedSkus'
url = 'subscribedSkus'
method = 'GET'
}
@{
id = 'directorySubscriptions'
url = 'directory/subscriptions'
method = 'GET'
}
@{
id = 'licensedUsers'
url = "users?`$select=id,displayName,userPrincipalName,assignedLicenses&`$filter=assignedLicenses/`$count ne 0&`$count=true"
method = 'GET'
headers = @{
'ConsistencyLevel' = 'eventual'
}
}
@{
id = 'licensedGroups'
url = "groups?`$select=id,displayName,assignedLicenses,mailEnabled,securityEnabled,groupTypes,onPremisesSyncEnabled&`$filter=assignedLicenses/`$count ne 0&`$count=true"
method = 'GET'
headers = @{
'ConsistencyLevel' = 'eventual'
}
}
)
try {
$AdminPortalLicenses = New-GraphGetRequest -scope 'https://admin.microsoft.com/.default' -TenantID $TenantFilter -Uri 'https://admin.microsoft.com/fd/m365licensing/v3/licensedProducts?allotmentSourceOwnerType=User&allotmentSourceType=LowFrictionTrial&allotmentSourceState=Active,Deleted,Suspended,Lockout,Warning&displayNameLanguage=en-GB'
} catch {
Write-Warning "Failed to get Admin Portal Licenses: $($_.Exception.Message)"
$AdminPortalLicenses = @()
}
$Results = New-GraphBulkRequest -Requests $Requests -TenantID $TenantFilter -asapp $true
$LicRequest = ($Results | Where-Object { $_.id -eq 'subscribedSkus' }).body.value
$SkuIDs = ($Results | Where-Object { $_.id -eq 'directorySubscriptions' }).body.value
$RawGraphRequest = [PSCustomObject]@{
Tenant = $TenantFilter
Licenses = $LicRequest
}
$ConvertTable = [System.IO.File]::ReadAllText((Join-Path $env:CIPPRootPath 'Config\ConversionTable.csv')) | ConvertFrom-Csv
$LicenseTable = Get-CIPPTable -TableName ExcludedLicenses
$ExcludedSkuList = Get-CIPPAzDataTableEntity @LicenseTable
# If no excluded licenses exist, initialize them
if ($ExcludedSkuList.Count -lt 1) {
Write-Information 'Excluded licenses table is empty. Initializing from config file.'
$null = Initialize-CIPPExcludedLicenses
$ExcludedSkuList = Get-CIPPAzDataTableEntity @LicenseTable
}
$AllLicensedUsers = @(($Results | Where-Object { $_.id -eq 'licensedUsers' }).body.value) | Sort-Object -Property displayName
$UsersBySku = @{}
foreach ($User in $AllLicensedUsers) {
if (-not $User.assignedLicenses) { continue } # Skip users with no assigned licenses. Should not happens as the filter is applied, but just in case
$UserInfo = [PSCustomObject]@{
displayName = [string]$User.displayName
userPrincipalName = [string]$User.userPrincipalName
id = [string]$User.id
}
foreach ($AssignedLicense in $User.assignedLicenses) {
$LicenseSkuId = ([string]$AssignedLicense.skuId).ToLowerInvariant()
if ([string]::IsNullOrWhiteSpace($LicenseSkuId)) { continue } # Skip if SKU ID is null or whitespace. Should not happen but just in case
if (-not $UsersBySku.ContainsKey($LicenseSkuId)) {
$UsersBySku[$LicenseSkuId] = [System.Collections.Generic.List[object]]::new()
}
$UsersBySku[$LicenseSkuId].Add($UserInfo)
}
}
$AllLicensedGroups = @(($Results | Where-Object { $_.id -eq 'licensedGroups' }).body.value) | Sort-Object -Property displayName
$GroupsBySku = @{}
foreach ($Group in $AllLicensedGroups) {
if (-not $Group.assignedLicenses) { continue }
$GroupInfo = [PSCustomObject]@{
displayName = [string]$Group.displayName
calculatedGroupType = if ($Group.groupTypes -contains 'Unified') { 'Microsoft 365' }
elseif ($Group.mailEnabled -and $Group.securityEnabled) { 'Mail-Enabled Security' }
elseif (-not $Group.mailEnabled -and $Group.securityEnabled) { 'Security' }
elseif (([string]::isNullOrEmpty($Group.groupTypes)) -and ($Group.mailEnabled) -and (-not $Group.securityEnabled)) { 'Distribution List' }
id = [string]$Group.id
onPremisesSyncEnabled = [bool]$Group.onPremisesSyncEnabled
}
foreach ($AssignedLicense in $Group.assignedLicenses) {
$LicenseSkuId = ([string]$AssignedLicense.skuId).ToLowerInvariant()
if ([string]::IsNullOrWhiteSpace($LicenseSkuId)) { continue }
if (-not $GroupsBySku.ContainsKey($LicenseSkuId)) {
$GroupsBySku[$LicenseSkuId] = [System.Collections.Generic.List[object]]::new()
}
$GroupsBySku[$LicenseSkuId].Add($GroupInfo)
}
}
$GraphRequest = foreach ($singleReq in $RawGraphRequest) {
$skuId = $singleReq.Licenses
foreach ($sku in $skuId) {
if ($sku.skuId -in $ExcludedSkuList.GUID) { continue }
$PrettyNameAdmin = $AdminPortalLicenses | Where-Object { $_.aadSkuId -eq $sku.skuId } | Select-Object -ExpandProperty displayName -First 1
$PrettyNameCSV = ($ConvertTable | Where-Object { $_.guid -eq $sku.skuid }).'Product_Display_Name' | Select-Object -Last 1
$PrettyName = $PrettyNameAdmin ?? $PrettyNameCSV ?? $sku.skuPartNumber
# Initialize $Term with the default value
$TermInfo = foreach ($Subscription in $sku.subscriptionIds) {
$SubInfo = $SkuIDs | Where-Object { $_.id -eq $Subscription }
$diff = $SubInfo.nextLifecycleDateTime - $SubInfo.createdDateTime
$Term = 'Term unknown or non-NCE license'
if ($diff.Days -ge 32 -and $diff.Days -le 1089) {
$Term = 'Yearly'
} elseif ($diff.Days -ge 1090 -and $diff.Days -le 1100) {
$Term = '3 Year'
} elseif ($diff.Days -ge 25 -and $diff.Days -le 31) {
$Term = 'Monthly'
}
$TimeUntilRenew = ($subinfo.nextLifecycleDateTime - (Get-Date)).days
[PSCustomObject]@{
Status = $SubInfo.status
Term = $Term
TotalLicenses = $SubInfo.totalLicenses
DaysUntilRenew = $TimeUntilRenew
NextLifecycle = $SubInfo.nextLifecycleDateTime
CreatedDateTime = $SubInfo.createdDateTime
IsTrial = $SubInfo.isTrial
SubscriptionId = $subinfo.id
CSPSubscriptionId = $SubInfo.commerceSubscriptionId
OCPSubscriptionId = $SubInfo.ocpSubscriptionId
}
}
$SkuKey = ([string]$sku.skuId).ToLowerInvariant()
[pscustomobject]@{
Tenant = [string]$singleReq.Tenant
License = [string]$PrettyName
CountUsed = [string]"$($sku.consumedUnits)"
CountAvailable = [string]$sku.prepaidUnits.enabled - $sku.consumedUnits
TotalLicenses = [string]"$($sku.prepaidUnits.enabled)"
skuId = [string]$sku.skuId
skuPartNumber = [string]$PrettyName
availableUnits = [string]$sku.prepaidUnits.enabled - $sku.consumedUnits
TermInfo = @($TermInfo)
AssignedUsers = ($UsersBySku.ContainsKey($SkuKey) ? @(($UsersBySku[$SkuKey])) : $null)
AssignedGroups = ($GroupsBySku.ContainsKey($SkuKey) ? @(($GroupsBySku[$SkuKey])) : $null)
ServicePlans = $sku.servicePlans
}
}
}
return ($GraphRequest | Sort-Object -Property License)
}