Skip to content

Commit 609e0d9

Browse files
authored
25535 - Refactor to fix subscription handling and error messaging (#904)
1 parent 48ea9d0 commit 609e0d9

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/powershell/tests/Test-Assessment.25535.ps1

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,28 +270,29 @@ function Test-Assessment-25535 {
270270
}
271271

272272
try {
273-
$accessToken = Get-AzAccessToken -AsSecureString -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
273+
$subscriptions = @(Get-AzSubscription -ErrorAction Stop | Where-Object { $_.Id -and $_.State -eq 'Enabled' })
274+
if ($subscriptions.Count -eq 0) { throw "No enabled Azure subscriptions found in the current context." }
274275
}
275276
catch {
276-
Write-PSFMessage $_.Exception.Message -Tag Test -Level Error
277-
}
278-
279-
if (-not $accessToken) {
280-
Write-PSFMessage "Azure authentication token not found." -Tag Test -Level Warning
277+
Write-PSFMessage $_.Exception.Message -Tag Test -Level Warning
281278
Add-ZtTestResultDetail -SkippedBecause 'NotConnectedAzure'
282279
return
283280
}
284281

285-
$subscriptions = Get-AzSubscription
286-
$firewalls = @()
287282
$nicFindings = @()
288283

289284
foreach ($sub in $subscriptions) {
290-
Set-AzContext -SubscriptionId $sub.Id | Out-Null
285+
try {
286+
Set-AzContext -SubscriptionId $sub.Id -ErrorAction Stop | Out-Null
287+
}
288+
catch {
289+
Write-PSFMessage "Skipping subscription $($sub.Name) ($($sub.Id)): $($_.Exception.Message)" -Tag Test -Level Warning
290+
continue
291+
}
291292

292293
# Collect firewall private IPs
293-
$firewalls += Get-FirewallPrivateIP -SubscriptionId $sub.Id
294-
if ($firewalls.Count -eq 0) { continue }
294+
$subscriptionFirewalls = @(Get-FirewallPrivateIP -SubscriptionId $sub.Id)
295+
if ($subscriptionFirewalls.Count -eq 0) { continue }
295296

296297
# Launch async operations for workload NICs
297298
$asyncOperations = Get-WorkloadNicOperation -Subscription $sub -SubscriptionId $sub.Id
@@ -304,7 +305,7 @@ function Test-Assessment-25535 {
304305

305306
# Process results into findings
306307
foreach ($op in $completedOperations) {
307-
$nicFindings += ConvertTo-NicFinding -Operation $op -Firewalls $firewalls
308+
$nicFindings += ConvertTo-NicFinding -Operation $op -Firewalls $subscriptionFirewalls
308309
}
309310
}
310311
#endregion Data Collection

0 commit comments

Comments
 (0)