Skip to content

Commit

Permalink
Add subnets to the resource list (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerJon authored Sep 4, 2024
1 parent cd8e471 commit 3a60da8
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions Scripts/Helpers/RestMethods/Get-AzResourceListRestMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@ function Get-AzResourceListRestMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
$SubscriptionId,

[string] $ApiVersion = "2021-04-01"
$SubscriptionId
)

$path = "/subscriptions/$SubscriptionId/resources?api-version=$ApiVersion"
$response = Invoke-AzRestMethod -Path $path -Method GET
function Exec-AzRestMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
$path,
[Parameter(Mandatory = $true)]
$method
)

$response = Invoke-AzRestMethod -Path $path -Method $method

# Process response
$statusCode = $response.StatusCode
if ($statusCode -lt 200 -or $statusCode -ge 300) {
$content = $response.Content
Write-Warning "Policy Exemption error for scope '$Scope' $($statusCode) -- $($content)"
Write-Output @() -NoEnumerate
}

# Process response
$statusCode = $response.StatusCode
if ($statusCode -lt 200 -or $statusCode -ge 300) {
$content = $response.Content
Write-Warning "Policy Exemption error for scope '$Scope' $($statusCode) -- $($content)"
Write-Output @() -NoEnumerate
$resources = $content | ConvertFrom-Json -Depth 100 -AsHashtable
$nextLink = ($response.Content | ConvertFrom-Json -Depth 100 -AsHashtable).nextLink
while ($null -ne $nextLink) {
$appendURL = (([uri]$nextlink).Query -split '&')[-1]
$response = Invoke-AzRestMethod -Path ($path + '&' + $appendURL) -Method GET
$resources.value += ($response.Content | ConvertFrom-Json -Depth 100 -AsHashtable).value
$nextLink = ($response.Content | ConvertFrom-Json -Depth 100 -AsHashtable).nextLink
}
return $resources
}

# Get the basic resources
$ApiVersion = "2021-04-01"
$path = "/subscriptions/$SubscriptionId/resources?api-version=$ApiVersion"
$resources = Exec-AzRestMethod -path $path -method GET

# Get the Subnets for all the Vnets found in the basic resources
$snets = $($resources.value | Where-Object { $_.type -eq 'Microsoft.Network/virtualNetworks' })
foreach ($snet in $snets) {
$ApiVersion = "2024-01-01"
$path = "$($snet.id)/subnets?api-version=$ApiVersion"
$subnetResources = Exec-AzRestMethod -path $path -method GET
$resources.value += $subnetResources.value
}

$content = $response.Content
$resources = $content | ConvertFrom-Json -Depth 100 -AsHashtable
$nextLink = ($response.Content | ConvertFrom-Json -Depth 100 -AsHashtable).nextLink
while ($null -ne $nextLink) {
$appendURL = (([uri]$nextlink).Query -split '&')[-1]
$response = Invoke-AzRestMethod -Path ($path + '&' + $appendURL) -Method GET
$resources.value += ($response.Content | ConvertFrom-Json -Depth 100 -AsHashtable).value
$nextLink = ($response.Content | ConvertFrom-Json -Depth 100 -AsHashtable).nextLink
}

Write-Output $resources.value -NoEnumerate
}

0 comments on commit 3a60da8

Please sign in to comment.