'Export Vnet | Subnet

I'd like to export something like the view of the subnets that exist in a VNET like is displayed in the portal. Unfortunately there isn't an option to export this to a CSV. I have found powershell scripts online that can export subnet route tables and the associated subnets. I have also found powershell scripts to export details on vnets subnets. However I haven't been able to find scripts that combine both

Script for Route tables by Aman Sharma

Ignore the synopsis and description I think he left them in from previous scripts

So I'm trying to reverse the logic i.e. get the subnet details and add the route tables for each subnet if it exists. However I'm not sure what I'm doing at this point! the script is erroring with:

Line |
  47 |  …         $routeTables = Get-AzRouteTable -Name $routeTableName -Resour …
     |                                                  ~~~~~~~~~~~~~~~
     | Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

The script ends and the CSV has everything but the route table details. So if you could help a noob I'd be very grateful here is what I have:

$PathToOutputCSVReport = "/path/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
    #Creating Output Object
    $results = @()

    #Iterating over various subscriptions
    foreach($sub in $subs)
    {
        $SubscriptionId = $sub.SubscriptionId
        Write-Output $SubscriptionName

        #Selecting the Azure Subscription
        Select-AzSubscription -SubscriptionName $SubscriptionId

        #Getting all Azure Route Tables
        $vnets = Get-AzVirtualNetwork

        foreach($vnet in $vnets)
        {
            $vnetName = $vnet.Name
            $vnetResourceGroup = $vnet.ResourceGroupName
            Write-Output $vnetName

            #Fetch Route Subnets
            $vnetSubnets = $vnet.Subnets

            foreach($vnetSubnet in $vnetSubnets)
            {
                $subnetName = $vnetSubnet.Name
                Write-Output $subnetName

                $subnetId = $vnetSubnet.Id

                ###Getting information
                $splitarray = $subnetId.Split('/')
                $subscriptionId = $splitarray[2]
                $vNetResourceGroupName = $splitarray[4]
                $virtualNetworkName = $splitarray[8]
                $subnetName = $splitarray[10]

                #Fetch the route table details
                $routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

                #Fetching the vNet and Subnet details
                #$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
                $subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
        
                $subnetAddressPrefix = $subnet.AddressPrefix[0]

                $details = @{            
                        virtualNetworkName=$virtualNetworkName
                        subnetAddressPrefix=$subnetAddressPrefix
                        subnetName=$subnetName
                        routeTableName=$routeTableName
                        routeResourceGroup=$routeResourceGroup
                        subscriptionId=$subscriptionId
                        vNetResourceGroupName=$vNetResourceGroupName
                        
                }                           
                $results += New-Object PSObject -Property $details
                

            }

        }
    }
    $results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
    Write-Host -ForegroundColor Red "No Subscription Found"
}


Solution 1:[1]

This error you're getting clearly shows that the variable routeTableName is an invalid value the -Name parameter for the cmdlet Get-AzRouteTable.

Looking at your script, it appears that the variable is not defined anywhere, which explains why its empty, hence invalid to be used with the cmdlet.

To define the variable with the name of the route table associated to a subnet, you can use the following:

$routeTableName = $subnet.RouteTable.Id.Split('/')[8]

It also seems you're not using the following at all and can be removed:

$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

Here's how your entire code would look like:

$PathToOutputCSVReport = "/path/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
    #Creating Output Object
    $results = @()

    #Iterating over various subscriptions
    foreach($sub in $subs)
    {
        $SubscriptionId = $sub.SubscriptionId
        Write-Output $SubscriptionName

        #Selecting the Azure Subscription
        Select-AzSubscription -SubscriptionName $SubscriptionId

        #Getting all Azure Route Tables
        $vnets = Get-AzVirtualNetwork

        foreach($vnet in $vnets)
        {
            $vnetName = $vnet.Name
            $vnetResourceGroup = $vnet.ResourceGroupName
            Write-Output $vnetName

            #Fetch Route Subnets
            $vnetSubnets = $vnet.Subnets

            foreach($vnetSubnet in $vnetSubnets)
            {
                $subnetName = $vnetSubnet.Name
                Write-Output $subnetName

                $subnetId = $vnetSubnet.Id

                ###Getting information
                $splitarray = $subnetId.Split('/')
                $subscriptionId = $splitarray[2]
                $vNetResourceGroupName = $splitarray[4]
                $virtualNetworkName = $splitarray[8]
                $subnetName = $splitarray[10]

                #Fetch the route table details
                #$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

                #Fetching the vNet and Subnet details
                #$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
                $subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
                $routeTableName = $subnet.RouteTable.Id.Split('/')[8]
        
                $subnetAddressPrefix = $subnet.AddressPrefix[0]

                $details = @{            
                        virtualNetworkName=$virtualNetworkName
                        subnetAddressPrefix=$subnetAddressPrefix
                        subnetName=$subnetName
                        routeTableName=$routeTableName
                        routeResourceGroup=$routeResourceGroup
                        subscriptionId=$subscriptionId
                        vNetResourceGroupName=$vNetResourceGroupName
                        
                }                           
                $results += New-Object PSObject -Property $details
                

            }

        }
    }
    $results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
    Write-Host -ForegroundColor Red "No Subscription Found"
}

Solution 2:[2]

We have tried the same and getting the same warning as yours ,

enter image description here

Looking at your script ,you are doing correct but have not provided any variable to read the $routeTableName or $routeResourceGroup at line 47. We have used foreach loop again to retrieve route table details as subnet and vnets you have used, Followed by the below script we are able to run it without any failures:-

$PathToOutputCSVReport = "/mylocalpath/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
    #Creating Output Object
    $results = @()

    #Iterating over various subscriptions
    foreach($sub in $subs)
    {
        $SubscriptionId = $sub.SubscriptionId
        Write-Output $SubscriptionName

        #Selecting the Azure Subscription
        Select-AzSubscription -SubscriptionName $SubscriptionId

        #Getting all Azure Route Tables
        $vnets = Get-AzVirtualNetwork

        foreach($vnet in $vnets)
        {
            $vnetName = $vnet.Name
            $vnetResourceGroup = $vnet.ResourceGroupName
            Write-Output $vnetName

            #Fetch Route Subnets
            $vnetSubnets = $vnet.Subnets

            foreach($vnetSubnet in $vnetSubnets)
            {
                $subnetName = $vnetSubnet.Name
                Write-Output $subnetName

                $subnetId = $vnetSubnet.Id

                ###Getting information
                $splitarray = $subnetId.Split('/')
                $subscriptionId = $splitarray[2]
                $vNetResourceGroupName = $splitarray[4]
                $virtualNetworkName = $splitarray[8]
                $subnetName = $splitarray[10]

                #Fetch the route table details
                #$routeTables = Get-AzRouteTable -ResourceGroupName $routeResourceGroup -Name $routeTableName instead of this tried below line to fetch the route table details
                
        $routeTables = Get-AzRouteTable

        foreach($routeTable in $routeTables)
        {
            $routeTableName = $routeTable.Name
            $routeResourceGroup = $routeTable.ResourceGroupName
            Write-Output $routeName 

                #Fetching the vNet and Subnet details
                #$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
                $subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
        
                $subnetAddressPrefix = $subnet.AddressPrefix[0]

                $details = @{            
                        virtualNetworkName=$virtualNetworkName
                        subnetAddressPrefix=$subnetAddressPrefix
                        subnetName=$subnetName
                        routeTableName=$routeTableName
                        routeResourceGroup=$routeResourceGroup
                        subscriptionId=$subscriptionId
                        vNetResourceGroupName=$vNetResourceGroupName
                        
                }                           
                $results += New-Object PSObject -Property $details
                
               }
            }

        }
    }
    $results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
    Write-Host -ForegroundColor Red "No Subscription Found"
}

Also the link which you have shared it will also list as per your requirement .

enter image description here OUTPUT DETAILS FOR REFERENCE:-

enter image description here

CSV FILE :-

enter image description here

For more information to fetch the route details in different way as well please refer the below links:-

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ked Mardemootoo
Solution 2