Azure subnets: export for phpIPAM

Overview

This PowerShell script exports Azure virtual network subnets into a CSV format that can be imported into phpIPAM.

It loops through all Azure subscriptions available to the current account, reads every VNet and subnet, and writes the fields expected by a phpIPAM subnet import template:

  • Section
  • Subnet
  • Mask
  • Description
  • VLAN
  • Domain
  • VRF
  • custom_Region

The Description field is built from the Azure subscription, VNet, and subnet names so the subnet can be traced back to Azure after import.

Prerequisites

Run the script from Azure Cloud Shell or from a machine with the Azure PowerShell module installed.

If running locally, install the Azure PowerShell module if needed:

Install-Module Az -Scope CurrentUser

Connect to Azure before running the script:

Connect-AzAccount

The account must have permission to read virtual networks and subnets in each subscription that should be exported.

Output

The CSV is written to the current directory with a timestamped filename:

./phpipam-azure-subnets-yyyyMMdd-HHmmss.csv

Example output columns:

Section,Subnet,Mask,Description,VLAN,Domain,VRF,custom_Region

PowerShell script

# ============================================================
# Azure Subnets -> phpIPAM CSV Export
# Loops through ALL accessible Azure subscriptions
# Designed for Azure Cloud Shell / existing Az login
#
# phpIPAM template columns:
# Section,Subnet,Mask,Description,VLAN,Domain,VRF,custom_Region
# ============================================================
 
$Section = Read-Host "Enter the phpIPAM Section"
 
if ([string]::IsNullOrWhiteSpace($Section)) {
    Write-Error "Section cannot be empty."
    exit 1
}
 
# Get every subscription accessible to the current account
$subscriptions = Get-AzSubscription
 
if (-not $subscriptions) {
    Write-Error "No Azure subscriptions found."
    exit 1
}
 
$results = @()
 
foreach ($subscription in $subscriptions) {
 
    Write-Host ""
    Write-Host "Processing subscription: $($subscription.Name)"
    Write-Host "Subscription ID: $($subscription.Id)"
 
    try {
        Set-AzContext -SubscriptionId $subscription.Id -ErrorAction Stop | Out-Null
    }
    catch {
        Write-Warning "Could not switch to subscription '$($subscription.Name)'. Skipping."
        continue
    }
 
    try {
        $vnets = Get-AzVirtualNetwork -ErrorAction Stop
    }
    catch {
        Write-Warning "Could not retrieve VNets from '$($subscription.Name)'. Skipping."
        continue
    }
 
    if (-not $vnets) {
        Write-Host "No VNets found."
        continue
    }
 
    foreach ($vnet in $vnets) {
 
        foreach ($subnet in $vnet.Subnets) {
 
            $prefixes = @($subnet.AddressPrefix)
 
            foreach ($prefix in $prefixes) {
 
                if ([string]::IsNullOrWhiteSpace($prefix)) {
                    continue
                }
 
                $parts = $prefix -split "/"
 
                if ($parts.Count -ne 2) {
                    Write-Warning "Could not parse prefix '$prefix' in subnet '$($subnet.Name)'"
                    continue
                }
 
                $results += [PSCustomObject]@{
                    Section       = $Section
                    Subnet        = $parts[0]
                    Mask          = $parts[1]
                    Description   = "$($subscription.Name) / $($vnet.Name) / $($subnet.Name)"
                    VLAN          = ""
                    Domain        = ""
                    VRF           = ""
                    custom_Region = $vnet.Location
                }
            }
        }
    }
}
 
if (-not $results) {
    Write-Warning "No Azure subnets were found across any subscription."
    exit 0
}
 
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$outputFile = "./phpipam-azure-subnets-$timestamp.csv"
 
$results |
    Sort-Object Section, custom_Region, Description, Subnet |
    Export-Csv `
        -Path $outputFile `
        -NoTypeInformation `
        -Encoding UTF8
 
Write-Host ""
Write-Host "Export complete."
Write-Host "Subscriptions checked: $($subscriptions.Count)"
Write-Host "Subnets exported:      $($results.Count)"
Write-Host "CSV file:              $outputFile"
Write-Host ""
 
$results |
    Format-Table Section, Subnet, Mask, Description, custom_Region -AutoSize

Notes

  • The phpIPAM Section value is entered interactively when the script starts.
  • Subnet and Mask are split from the Azure CIDR prefix, for example 10.10.1.0/24.
  • custom_Region is populated from the Azure VNet location.
  • VLAN, Domain, and VRF are left empty so they can be handled in phpIPAM if needed.
  • If a subscription cannot be queried, the script warns and continues with the remaining subscriptions.