Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created September 29, 2022 14:15
Show Gist options
  • Save JustinGrote/14eb3fb254839ed80003928c7699b18b to your computer and use it in GitHub Desktop.
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
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