-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathTest-Assessment.25375.ps1
More file actions
339 lines (279 loc) · 12.7 KB
/
Test-Assessment.25375.ps1
File metadata and controls
339 lines (279 loc) · 12.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<#
.SYNOPSIS
Validates that GSA licenses are available in the tenant and assigned to users.
.DESCRIPTION
This test checks whether Global Secure Access (GSA) licenses are provisioned in the tenant
and actively assigned to users. It verifies:
- GSA service plans exist in tenant subscribed SKUs
- Licenses have capabilityStatus = "Enabled"
- Licenses are assigned to at least one user
- Service plans are not disabled for assigned users
.NOTES
Test ID: 25375
Category: Global Secure Access
Required API: subscribedSkus (beta)
Required Database: User table
GSA Service Plan IDs:
- Entra_Premium_Internet_Access: 8d23cb83-ab07-418f-8517-d7aca77307dc
- Entra_Premium_Private_Access: f057aab1-b184-49b2-85c0-881b02a405c5
#>
function Test-Assessment-25375 {
[ZtTest(
Category = 'Global Secure Access',
ImplementationCost = 'Low',
MinimumLicense = ('Entra_Premium_Internet_Access', 'Entra_Premium_Private_Access'),
Pillar = 'Network',
RiskLevel = 'High',
SfiPillar = 'Protect networks',
TenantType = ('Workforce'),
TestId = 25375,
Title = 'GSA Licenses are available in the tenant and assigned to users',
UserImpact = 'Low'
)]
[CmdletBinding()]
param(
$Database
)
#region Data Collection
Write-PSFMessage '🟦 Start' -Tag Test -Level VeryVerbose
$activity = 'Checking GSA license availability and assignment'
Write-ZtProgress -Activity $activity -Status 'Querying tenant licenses'
# GSA Service Plan IDs
$gsaServicePlanIds = @{
InternetAccess = '8d23cb83-ab07-418f-8517-d7aca77307dc' # Entra_Premium_Internet_Access
PrivateAccess = 'f057aab1-b184-49b2-85c0-881b02a405c5' # Entra_Premium_Private_Access
}
$skuCmdletFailed = $false
$userCmdletFailed = $false
$subscribedSkus = @()
$userLicenses = @()
# Query 1: Retrieve tenant licenses with GSA service plans
try {
$subscribedSkus = Invoke-ZtGraphRequest -RelativeUri 'subscribedSkus' -ApiVersion beta -ErrorAction Stop
}
catch {
$skuCmdletFailed = $true
Write-PSFMessage "Failed to retrieve subscribed SKUs: $_" -Tag Test -Level Warning
}
Write-ZtProgress -Activity $activity -Status 'Querying user license assignments'
# Query 2: Retrieve all users with assigned licenses from database
try {
$sqlUsers = @"
SELECT
u.id,
u.displayName,
u.userPrincipalName,
unnest(u.assignedLicenses).skuId::VARCHAR AS skuId,
unnest(u.assignedLicenses).disabledPlans AS disabledPlans
FROM "User" u
WHERE len(u.assignedLicenses) > 0
"@
$userLicenses = @(Invoke-DatabaseQuery -Database $Database -Sql $sqlUsers -AsCustomObject -ErrorAction Stop)
# Filter out any records with null IDs
$userLicenses = @($userLicenses | Where-Object { $_.id })
}
catch {
$userCmdletFailed = $true
Write-PSFMessage "Failed to retrieve users: $_" -Tag Test -Level Warning
}
#endregion Data Collection
#region Assessment Logic
$testResultMarkdown = ''
$passed = $false
$customStatus = $null
# Handle any query failure - cannot determine license status
if ($skuCmdletFailed -or $userCmdletFailed) {
Write-PSFMessage "Unable to retrieve GSA license data due to query failure" -Tag Test -Level Warning
$customStatus = 'Investigate'
$testResultMarkdown = "⚠️ Unable to determine GSA license availability and assignment due to query failure, connection issues, or insufficient permissions.`n`n"
Add-ZtTestResultDetail -TestId '25375' -Title 'GSA Licenses are available in the tenant and assigned to users' -Status $false -Result $testResultMarkdown -CustomStatus $customStatus
return
}
# Filter SKUs containing GSA service plans
$gsaSkus = @($subscribedSkus | Where-Object {
$_.ServicePlans | Where-Object { $_.ServicePlanId -in $gsaServicePlanIds.Values }
})
# Check if GSA licenses exist and are enabled
$enabledGsaSkus = @($gsaSkus | Where-Object { $_.CapabilityStatus -eq 'Enabled' })
if ($gsaSkus.Count -eq 0 -or $enabledGsaSkus.Count -eq 0) {
# No GSA licenses available or not enabled - skip test
Write-PSFMessage 'No GSA licenses are available in this tenant.' -Tag Test -Level Verbose
Add-ZtTestResultDetail -SkippedBecause NotApplicable -Result 'No GSA licenses are available in this tenant.'
return
}
Write-ZtProgress -Activity $activity -Status 'Analyzing user license assignments'
# Build SKU ID to SKU mapping and pre-filter service plans for performance
$gsaSkuIds = @{}
$internetAccessPlansBySku = @{}
$privateAccessPlansBySku = @{}
foreach ($sku in $enabledGsaSkus) {
$skuIdString = $sku.SkuId.ToString().ToLower()
$gsaSkuIds[$skuIdString] = $sku
# Pre-filter service plans to avoid repeated Where-Object calls
$internetPlan = $sku.ServicePlans | Where-Object { $_.ServicePlanId -eq $gsaServicePlanIds.InternetAccess }
if ($internetPlan) {
$internetAccessPlansBySku[$skuIdString] = $internetPlan
}
$privatePlan = $sku.ServicePlans | Where-Object { $_.ServicePlanId -eq $gsaServicePlanIds.PrivateAccess }
if ($privatePlan) {
$privateAccessPlansBySku[$skuIdString] = $privatePlan
}
}
# Count users with GSA licenses assigned
$usersWithInternetAccess = [System.Collections.Generic.List[object]]::new()
$usersWithPrivateAccess = [System.Collections.Generic.List[object]]::new()
$usersWithAnyGsa = [System.Collections.Generic.List[object]]::new()
# Group licenses by user (since query returns one row per license)
$userGroups = $userLicenses | Group-Object -Property id
foreach ($userGroup in $userGroups) {
$userId = $userGroup.Name
$userLicenseRecords = $userGroup.Group
$userDisplayName = $userLicenseRecords[0].displayName
$userPrincipalName = $userLicenseRecords[0].userPrincipalName
$hasInternetAccess = $false
$hasPrivateAccess = $false
foreach ($licenseRecord in $userLicenseRecords) {
if (-not $licenseRecord.skuId) { continue }
$userSkuId = $licenseRecord.skuId.ToString().ToLower()
if ($gsaSkuIds.ContainsKey($userSkuId)) {
$disabledPlans = if ($licenseRecord.disabledPlans) { $licenseRecord.disabledPlans } else { @() }
# Check if Internet Access service plan is enabled
if ($internetAccessPlansBySku.ContainsKey($userSkuId)) {
$internetPlan = $internetAccessPlansBySku[$userSkuId]
if ($internetPlan.ServicePlanId -notin $disabledPlans) {
$hasInternetAccess = $true
}
}
# Check if Private Access service plan is enabled
if ($privateAccessPlansBySku.ContainsKey($userSkuId)) {
$privatePlan = $privateAccessPlansBySku[$userSkuId]
if ($privatePlan.ServicePlanId -notin $disabledPlans) {
$hasPrivateAccess = $true
}
}
}
}
# Create user object for display
$userObj = [PSCustomObject]@{
Id = $userId
DisplayName = $userDisplayName
UserPrincipalName = $userPrincipalName
}
if ($hasInternetAccess) {
$usersWithInternetAccess.Add($userObj)
}
if ($hasPrivateAccess) {
$usersWithPrivateAccess.Add($userObj)
}
if ($hasInternetAccess -or $hasPrivateAccess) {
$usersWithAnyGsa.Add($userObj)
}
}
$gsaUserCount = $usersWithAnyGsa.Count
# Evaluate test result
if ($gsaUserCount -eq 0) {
# Licenses exist and enabled but not assigned to any user - fail
$passed = $false
$testResultMarkdown = "❌ GSA licenses are available in the tenant but not assigned to any user.`n`n%TestResult%"
}
else {
# Licenses exist, enabled, and assigned to at least one user - pass
$passed = $true
$testResultMarkdown = "✅ GSA licenses are available and assigned to at least one user.`n`n%TestResult%"
}
#endregion Assessment Logic
#region Report Generation
# Build detailed information if we have valid license data
$mdInfo = ''
if ($null -ne $enabledGsaSkus -and $enabledGsaSkus.Count -gt 0) {
$reportTitle = 'Licenses'
$portalLink = 'https://admin.microsoft.com/Adminportal/Home#/licenses'
$formatTemplate = @'
## [{0}]({1})
**GSA License Summary:**
| SKU Name | Status | Available | Assigned |
| :------- | :----- | --------: | -------: |
{2}
**GSA Service Plans Detected:**
| Service Plan | SKU |
| :----------- | :-- |
{3}
**User Assignment Summary:**
| Metric | Value |
| :----- | ----: |
{4}
{5}
'@
# Build SKU table
$skuTableRows = ''
foreach ($sku in $enabledGsaSkus) {
$skuName = Get-SafeMarkdown -Text $sku.SkuPartNumber
$status = Get-SafeMarkdown -Text $sku.CapabilityStatus
$available = $sku.PrepaidUnits.Enabled
$assigned = $sku.ConsumedUnits
$skuTableRows += "| $skuName | $status | $available | $assigned |`n"
}
# Build service plan table
$servicePlanTableRows = ''
foreach ($sku in $enabledGsaSkus) {
$gsaPlans = $sku.ServicePlans | Where-Object { $_.ServicePlanId -in $gsaServicePlanIds.Values }
foreach ($plan in $gsaPlans) {
$planName = Get-SafeMarkdown -Text $plan.ServicePlanName
$skuName = Get-SafeMarkdown -Text $sku.SkuPartNumber
$servicePlanTableRows += "| $planName | $skuName |`n"
}
}
# Build user assignment summary
$assignmentSummary = "| Users with GSA Internet Access | $($usersWithInternetAccess.Count) |`n"
$assignmentSummary += "| Users with GSA Private Access | $($usersWithPrivateAccess.Count) |`n"
$assignmentSummary += "| Total users with any GSA license | $gsaUserCount |`n"
# Build user list (truncate at 10)
$userListSection = ''
if ($gsaUserCount -gt 0) {
if ($gsaUserCount -gt 10) {
$userListSection += "**Users with GSA licenses (Showing 10 of $gsaUserCount):**`n`n"
}
else {
$userListSection += "**Users with GSA licenses:**`n`n"
}
$userListSection += "| Display name | User principal name | Internet Access | Private Access |`n"
$userListSection += "| :----------- | :------------------ | :-------------- | :------------- |`n"
# Build HashSets for efficient ID lookups
if ($usersWithInternetAccess.Count -gt 0) {
$internetAccessIds = [System.Collections.Generic.HashSet[string]]::new([string[]]($usersWithInternetAccess.Id))
} else {
$internetAccessIds = [System.Collections.Generic.HashSet[string]]::new()
}
if ($usersWithPrivateAccess.Count -gt 0) {
$privateAccessIds = [System.Collections.Generic.HashSet[string]]::new([string[]]($usersWithPrivateAccess.Id))
} else {
$privateAccessIds = [System.Collections.Generic.HashSet[string]]::new()
}
$displayUsers = $usersWithAnyGsa | Select-Object -First 10
foreach ($user in $displayUsers) {
$displayName = Get-SafeMarkdown -Text $user.DisplayName
$upn = Get-SafeMarkdown -Text $user.UserPrincipalName
$hasInternet = if ($internetAccessIds.Contains($user.Id)) { '✅' } else { '❌' }
$hasPrivate = if ($privateAccessIds.Contains($user.Id)) { '✅' } else { '❌' }
$userListSection += "| $displayName | $upn | $hasInternet | $hasPrivate |`n"
}
if ($gsaUserCount -gt 10) {
$userListSection += "| ... | | | |`n`n"
$userListSection += "View all users in [Microsoft 365 admin center - Licenses](https://admin.microsoft.com/Adminportal/Home#/licenses)"
}
}
$mdInfo = $formatTemplate -f $reportTitle, $portalLink, $skuTableRows, $servicePlanTableRows, $assignmentSummary, $userListSection
}
$testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $mdInfo
#endregion Report Generation
$params = @{
TestId = '25375'
Title = 'GSA Licenses are available in the tenant and assigned to users'
Status = $passed
Result = $testResultMarkdown
}
if ($customStatus) {
$params.CustomStatus = $customStatus
}
Add-ZtTestResultDetail @params
}