Created
September 29, 2022 14:15
-
-
Save JustinGrote/14eb3fb254839ed80003928c7699b18b to your computer and use it in GitHub Desktop.
Fetch all routes for all Azure subnets that have a route table attached
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace Microsoft.Azure.Commands.Profile.Models.PSAzureSubscription | |
using namespace Microsoft.Azure.Management.Internal.Resources.Utilities.Models | |
param( | |
$Subscriptions = $(Get-AzSubscription) | |
) | |
$Subscriptions | |
| Where-Object State -ne 'Disabled' | |
| ForEach-Object { | |
$context = Select-AzSubscription -SubscriptionID $PSItem.Id | |
$rtables = Get-AzRouteTable | |
foreach ($table in $rtables) { | |
$subnets = $table.Subnets | |
if (-not $subnets) { | |
Write-Warning "$($table.Id) has no associated subnets, skipping..." | |
continue | |
} | |
$routes = Get-AzRouteConfig -RouteTable $table | |
foreach ($subnet in $subnets) { | |
[ResourceIdentifier]$subnetId = $subnet.Id | |
foreach ($route in $routes) { | |
[PSCustomObject]@{ | |
Subscription = $context.Subscription.Name | |
VirtualNetwork = $subnetId.ParentResource.split('/')[1] | |
Subnet = $subnetId.ResourceName | |
RouteTable = $table.Name | |
RouteName = $route.Name | |
Destination = $route.AddressPrefix | |
NextHopType = $route.NextHopType | |
NextHop = $route.NextHopIpAddress | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment