Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Gather Azure Virtual Network inventory information using PowerShell

I am huge fan of PowerShell when gathering inventory report information. Before disabling few of my subscriptions in my Azure accounts, I wanted to gather Virtual Network inventory in the CSV file so It can be used later to see the information quickly to share in table format. Below is the screenshot of the information which will stored inside the csv file.

Microsoft Azure Powershell Virtual Network select-object resource group address space subnets vnet peering enableddosprotection get-azvirtualnetwork foreach loop.png

Download this script Get-AzureVirtualNetworkInfo.ps1 here or it is also available on github.com.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#created by: janvi | vcloud-lab.com
#date created: 10 December 2021
#purpose: Gather Azure Virtual Network information in CSV file

$vNets = Get-AzVirtualNetwork

foreach ($vNet in $vNets) {
    $subnetList = @()
    foreach ($subnet in $vNet.Subnets) {
        $subnetList += "[ $($subnet.Name) | $($vNet.Subnets.AddressPrefix -join ', ') ] "
    }

    $vnetPeeringList = @()
    foreach ($vnetPeering in $vNet.VirtualNetworkPeerings) {
        $remoteVNET =  $vNet.VirtualNetworkPeerings.RemoteVirtualNetwork
        $remoteVNETName = ($remoteVNET | foreach-Object {Split-Path $_.Id -leaf}) -join ', '
        $remoteVNETAddressSpace = $vNet.VirtualNetworkPeerings.RemoteVirtualNetworkAddressSpace.AddressPrefixes -join ', '
        $vnetPeeringList += "[ $($vnetPeering.Name) | $($vnetPeering.PeeringState) | $remoteVNETName | $remoteVNETAddressSpace ] "
    }

    [PSCustomObject]@{
        Name = $vNet.Name
        ResourceGroupName = $vNet.ResourceGroupName
        Location = $vNet.Location
        Id = $vNet.Id
        AddressSpace =  $vNet.AddressSpace.AddressPrefixes -join ', '
        Subnets = $subnetList -join ', '
        VnetPeerings = $vnetPeeringList -join ', '
        EnableDdosProtection = $vNet.EnableDdosProtection
    }
}

Useful Articles
Get Azure virtual machine backup reports using Powershell
Create a Virtual machine on Microsoft Azure
PowerShell List All Azure Resverations
Powershell get the list of Azure Reservations Virtual Machines instances
Get the list Azure Reservation Catalog with PowerShell and AzureCLI
Azure automation account DSC for On-Premise Virtual Machine on boarding
Azure Powershell : Operation returned an invalid status code 'BadRequest'
Create an Azure Automation account on using Portal PowerShell and AzureCLI

 

Go Back



Comment

Blog Search

Page Views

6687446

Follow me on Blogarama