From d7b23829b11977a54162ef2e4d109ba64df12af8 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 26 May 2026 16:04:21 -0400 Subject: [PATCH] Handle VNet JSON casing collisions with Resource Graph fallback --- pwsh/AzGovVizParallel.ps1 | 37 ++++++++++++++++++- .../dataCollectionFunctions.ps1 | 37 ++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/pwsh/AzGovVizParallel.ps1 b/pwsh/AzGovVizParallel.ps1 index a06b881..d2b938b 100644 --- a/pwsh/AzGovVizParallel.ps1 +++ b/pwsh/AzGovVizParallel.ps1 @@ -31829,9 +31829,42 @@ function dataCollectionVNets { $currentTask = "Getting Virtual Networks for Subscription: '$($scopeDisplayName)' ('$scopeId') [quotaId:'$SubscriptionQuotaId']" $uri = "$($azAPICallConf['azAPIEndpointUrls'].ARM)/subscriptions/$($scopeId)/providers/Microsoft.Network/virtualNetworks?api-version=2022-05-01" $method = 'GET' - $networkResult = AzAPICall -AzAPICallConfiguration $azAPICallConf -uri $uri -method $method -currentTask $currentTask -caller 'CustomDataCollection' - if ($networkResult -eq 'someError') { + try { + $networkResult = AzAPICall -AzAPICallConfiguration $azAPICallConf -uri $uri -method $method -currentTask $currentTask -caller 'CustomDataCollection' -unhandledErrorAction Continue + } + catch { + Write-Host "[dataCollectionVNets] Primary VNet collection failed for subscription '$scopeDisplayName' ('$scopeId'): $($_.Exception.Message)" + Write-Host "[dataCollectionVNets] Trying Resource Graph fallback for subscription '$scopeDisplayName' ('$scopeId')" + + try { + $uriResourceGraph = "$($azAPICallConf['azAPIEndpointUrls'].ARM)/providers/Microsoft.ResourceGraph/resources?api-version=2022-10-01" + $resourceGraphQuery = @" +resources +| where type =~ 'microsoft.network/virtualnetworks' +| where subscriptionId =~ '$scopeId' +| project id, name, location, properties +"@ + $body = @" +{ + "query": "$resourceGraphQuery", + "subscriptions": ["$scopeId"], + "options": { + "`$top": 1000 + } +} +"@ + + $networkResult = AzAPICall -AzAPICallConfiguration $azAPICallConf -uri $uriResourceGraph -method 'POST' -body $body -listenOn 'Content' -currentTask "$currentTask (ResourceGraph fallback)" -caller 'CustomDataCollection' -unhandledErrorAction Continue + Write-Host "[dataCollectionVNets] Resource Graph fallback succeeded for subscription '$scopeDisplayName' ('$scopeId') and returned $($networkResult.Count) Virtual Networks" + } + catch { + Write-Host "[dataCollectionVNets] Skipping VNet collection for subscription '$scopeDisplayName' ('$scopeId') due to fallback error: $($_.Exception.Message)" + $networkResult = 'convertfromJSONError' + } + } + + if ($networkResult -eq 'someError' -or $networkResult -eq 'convertfromJSONError') { } else { if ($networkResult.Count -gt 0) { diff --git a/pwsh/dev/functions/dataCollection/dataCollectionFunctions.ps1 b/pwsh/dev/functions/dataCollection/dataCollectionFunctions.ps1 index b070447..53ee2f2 100644 --- a/pwsh/dev/functions/dataCollection/dataCollectionFunctions.ps1 +++ b/pwsh/dev/functions/dataCollection/dataCollectionFunctions.ps1 @@ -232,9 +232,42 @@ function dataCollectionVNets { $currentTask = "Getting Virtual Networks for Subscription: '$($scopeDisplayName)' ('$scopeId') [quotaId:'$SubscriptionQuotaId']" $uri = "$($azAPICallConf['azAPIEndpointUrls'].ARM)/subscriptions/$($scopeId)/providers/Microsoft.Network/virtualNetworks?api-version=2022-05-01" $method = 'GET' - $networkResult = AzAPICall -AzAPICallConfiguration $azAPICallConf -uri $uri -method $method -currentTask $currentTask -caller 'CustomDataCollection' - if ($networkResult -eq 'someError') { + try { + $networkResult = AzAPICall -AzAPICallConfiguration $azAPICallConf -uri $uri -method $method -currentTask $currentTask -caller 'CustomDataCollection' -unhandledErrorAction Continue + } + catch { + Write-Host "[dataCollectionVNets] Primary VNet collection failed for subscription '$scopeDisplayName' ('$scopeId'): $($_.Exception.Message)" + Write-Host "[dataCollectionVNets] Trying Resource Graph fallback for subscription '$scopeDisplayName' ('$scopeId')" + + try { + $uriResourceGraph = "$($azAPICallConf['azAPIEndpointUrls'].ARM)/providers/Microsoft.ResourceGraph/resources?api-version=2022-10-01" + $resourceGraphQuery = @" +resources +| where type =~ 'microsoft.network/virtualnetworks' +| where subscriptionId =~ '$scopeId' +| project id, name, location, properties +"@ + $body = @" +{ + "query": "$resourceGraphQuery", + "subscriptions": ["$scopeId"], + "options": { + "`$top": 1000 + } +} +"@ + + $networkResult = AzAPICall -AzAPICallConfiguration $azAPICallConf -uri $uriResourceGraph -method 'POST' -body $body -listenOn 'Content' -currentTask "$currentTask (ResourceGraph fallback)" -caller 'CustomDataCollection' -unhandledErrorAction Continue + Write-Host "[dataCollectionVNets] Resource Graph fallback succeeded for subscription '$scopeDisplayName' ('$scopeId') and returned $($networkResult.Count) Virtual Networks" + } + catch { + Write-Host "[dataCollectionVNets] Skipping VNet collection for subscription '$scopeDisplayName' ('$scopeId') due to fallback error: $($_.Exception.Message)" + $networkResult = 'convertfromJSONError' + } + } + + if ($networkResult -eq 'someError' -or $networkResult -eq 'convertfromJSONError') { } else { if ($networkResult.Count -gt 0) {