Home > Mobile >  Export Vnet | Subnet
Export Vnet | Subnet

Time:04-27

Hello this is my first post and I'm poor at using powershell.

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 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

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

  • Related