Get Azure Virtual Networks with PowerShell
Rate this post

I needed to make my life easier the other day as a colleague and I worked through setting up a Azure IaaS network topology to connect to an enterprise production network. One of our clients requirements meant that whilst we created the network sites, subnets and segments we needed to report on what we had created to verify it was correct. This simple task of viewing network names and associated subnets is currently missing from the Azure cmdlets, so we have pieced together this quick bit of re-usable code. There isn’t currently a nice way to do a ‘Get-AzureVirtualNetwork’ and instead you only have the portal(s) or a downloadable XML. This isn’t great if you would like to do some validation checks or even a basic CSV dump to report on current configuration to a network administrator.

I’ve only been able to test this on a few VNET configurations but it seems to do the job for me so far. I’m sure that you can add your own trickery to it if you like, but by the time I invest in doing so, Microsoft will have probably released an updated module that includes this functionality. So here it is in all its glory.

The output will give you a nice table that you can export as per below;

Get-AzureVirtualNetwork | FT

Filter to find reference to matching CIDR blocks;

Get-AzureVirtualNetwork | ? {$_.AddressPrefix -like "10.72.*"} | FT

Store the networks and re-use with other Azure cmdlets;

$APSites = (Get-AzureVirtualNetwork | ? {$_.AddressPrefix -like "10.72.*"})

Export the current configuration for a report;

Get-AzureVirtualNetwork | Export-CSV -Path "c:\Temp\AzureVirtualNetworkConfiguration.csv"

If you notice the code snippet I threw in another handy export for all local routes;

Get-AzureVirtualNetworkRoutes

This will be useful to see if you are missing a local route when the list starts getting large.

Category:
Azure Infrastructure, Cloud Infrastructure, PowerShell
Tags:
, , , , ,

Leave a Reply

  Subscribe  
newest oldest most voted
Notify of
Willem
Guest
Willem

Excellent work, as always. Suggestion: make sure the attributes always come out in the same order for easier viewing.

Deepu [Mangalore] (@deepumangalore)
Guest

Nice work!

minor bug: When I export it shows as “System.Object[]” for those values which are in a list. I have a list in “AddressPrefix”

vkalal
Guest

Hi, do you have anything similar for ARM.

Simon Waight
Guest

Hi Vishal. The best way to achieve this is using either the Resource Explorer or the new “Automation Script” setting that allows you to create an ARM template from an existing deployment (note that it doesn’t support all Azure capabilities just yet though). Simon.

Vivek
Guest
Vivek

how can i calculate the available ips in subnet??
$nic = Get-AzureRmVirtualNetwork -Name TST-VNET1 -ResourceGroupName TST-RG1
$subnets= $nic.Subnets
foreach( $subnet in $subnets)
{
$subnet.IpConfigurations.Count
}

Using this i can get the usable ip is there any way to get available ip in subnet

Lucian Franghiu
Admin

Hi Vivek,
This is what I use to see if and IP is available to be assigned:
Test-AzureRMStaticVNETIP -VNETname [name] -IPaddress [the one you want to test] | Select-Object -Property IsAvailable

Hope that helps!?

Follow Us!

Kloud Solutions Blog - Follow Us!