Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Azure – Get Virtual Networks

Posted on March 28, 2016March 28, 2016 By David Kittell

If you work in Azure these two functions are very valuable additions to your library.

Note: This majority of this code is copied from the reference below. I added Gateway Address to the Network Routes along with DNS

# Show All Networks
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"

# Get Network Routes
Get-AzureVirtualNetworkRoutes | FT
function Get-AzureVirtualNetwork
{            
        
        #Get the current Network Config
        $XML = Get-AzureVNetConfig 
        #Define it as XML
        [ xml ]$fileContents = $XML.XMLConfiguration

        
        #Get to the root of the Network Configuration
        $networks = $fileContents.DocumentElement.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite
        #How Many Sites?
        $SiteCount = ($fileContents.DocumentElement.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite.Name).Count
        # Start the count at 0 so we can work through the multi-dimensional arrays
        $Count = 0

#-------------------- Single Site
# Root network config is structured differently 

    if($SiteCount -eq 1)
    {


        $Site = @()

        While($Count -le $SiteCount -and $Count -ne $SiteCount)
            {
                
                #Start Site
                $Object = New-Object System.Object

                #Get The Network Site Name & Location - Static amoungst whole network site
                $Object | Add-Member -type NoteProperty -name "Name" -Value $Networks.Name
                $Object | Add-Member -type NoteProperty -name "Location" -Value $Networks.Location
                $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks.Name
                $Object | Add-Member -Type NoteProperty -name "Type" -Value "Site"
                $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $Networks.addressspace.AddressPrefix
                $Site += $Object
                
                #Get All the Subnets in the Network
                $SubnetCount = ($Networks.subnets.subnet.name).count
                if($SubnetCount -gt 0)
                    {
                        $SubNetCounts = 0
                        while($SubnetCounts -le $SubnetCount -and $SubnetCounts -ne $SubnetCount)
                        {
                        $Object = New-Object System.Object

                        if($SubNetCounts -eq 0)
                            {
                            
                                $SubnetNamesCount = ($networks.subnets.Subnet.name).count
                                if($SubnetNamesCount -gt 1)
                                {
                                $Object | Add-Member -type NoteProperty -name "Name" -Value $networks.subnets.Subnet.name[0]
                                }
                                else
                                {
                                $Object | Add-Member -type NoteProperty -name "Name" -Value $networks.subnets.Subnet.name
                                }
                            $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks.Name
                            $Object | Add-Member -type NoteProperty -name "Location" -Value $Networks.Location
                            $Object | Add-Member -Type NoteProperty -name "Type" -Value "Subnet"
                            
                                $PrefixCount = ($networks.subnets.Subnet.AddressPrefix).count
                                if($PrefixCount -gt 1)
                                {
                                $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $networks.subnets.Subnet.AddressPrefix[0]
                                }
                                else
                                {
                                $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $networks.subnets.Subnet.AddressPrefix
                                }

                            }
                            else
                            {
                            $Object | Add-Member -type NoteProperty -name "Name" -Value $networks.subnets.Subnet.name[$SubNetCounts]
                            $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks.Name
                            $Object | Add-Member -type NoteProperty -name "Location" -Value $Networks.Location
                            $Object | Add-Member -Type NoteProperty -name "Type" -Value "Subnet"
                            $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $networks.subnets.Subnet.AddressPrefix[$SubNetCounts]
                            }

                        $Site += $Object
                        $SubNetCounts++
                        }
                    }
            $Count++
            }
            
}

#--------------------End

#--------------------Sites > 1 

    if($SiteCount -gt 1)
    {


        $Site = @()
        While($Count -le $SiteCount -and $Count -ne $SiteCount)
            {
                
                #Start Site
                $Object = New-Object System.Object

                #Get The Network Site Name & Location - Static amoungst whole network site
                $Object | Add-Member -type NoteProperty -name "Name" -Value $Networks[$count].Name
                $Object | Add-Member -type NoteProperty -name "Location" -Value $Networks[$count].Location
                $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks[$count].Name
                $Object | Add-Member -Type NoteProperty -name "Type" -Value "Site"
                $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $Networks[$count].addressspace.AddressPrefix
                $Site += $Object

                #Get All the Subnets in the Network
                $SubnetCount = ($Networks[$count].subnets.subnet.name).count
                if($SubnetCount -gt 0)
                    {
                        $SubNetCounts = 0
                        while($SubnetCounts -le $SubnetCount -and $SubnetCounts -ne $SubnetCount)
                        {
                        $Object = New-Object System.Object

                        if($SubNetCounts -eq 0)
                            {
                            
                                $SubnetNamesCount = ($networks[$count].subnets.Subnet.name).count
                                if($SubnetNamesCount -gt 1)
                                {
                                $Object | Add-Member -type NoteProperty -name "Name" -Value $networks[$count].subnets.Subnet.name[0]
                                }
                                else
                                {
                                $Object | Add-Member -type NoteProperty -name "Name" -Value $networks[$count].subnets.Subnet.name
                                }
                            $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks[$count].Name
                            $Object | Add-Member -type NoteProperty -name "Location" -Value $Networks[$count].Location
                            $Object | Add-Member -Type NoteProperty -name "Type" -Value "Subnet"
                            
                                $PrefixCount = ($networks[$count].subnets.Subnet.AddressPrefix).count
                                if($PrefixCount -gt 1)
                                {
                                $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $networks[$count].subnets.Subnet.AddressPrefix[0]
                                }
                                else
                                {
                                $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $networks[$count].subnets.Subnet.AddressPrefix
                                }

                            }
                            else
                            {
                            $Object | Add-Member -type NoteProperty -name "Name" -Value $networks[$count].subnets.Subnet.name[$SubNetCounts]
                            $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks[$count].Name
                            $Object | Add-Member -type NoteProperty -name "Location" -Value $Networks[$count].Location
                            $Object | Add-Member -Type NoteProperty -name "Type" -Value "Subnet"
                            $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $networks[$count].subnets.Subnet.AddressPrefix[$SubNetCounts]
                            }

                        $Site += $Object
                        $SubNetCounts++
                        }
                    }
            $Count++
            }
#--------------------End
    }
$Site 

}


function Get-AzureVirtualNetworkRoutes
{
        #Get the current Network Config
        $XML = Get-AzureVNetConfig 
        
        #Define it as XML
        [ xml ]$fileContents = $XML.XMLConfiguration
        
        #Get the current Local Network  Config
        $networks = $fileContents.DocumentElement.VirtualNetworkConfiguration.LocalNetworkSites.LocalNetworkSite
        

        #How Many Sites
        $SiteCount = ($fileContents.DocumentElement.VirtualNetworkConfiguration.LocalNetworkSites.LocalNetworkSite.Name).Count
           

        $Count = 0

#--------------------Get the local site routes

    if($SiteCount -eq 1)
    {

        $LocalSite = @()

        While($Count -le $SiteCount -and $Count -ne $SiteCount)
            {


                #Get All the Subnets in the Network
                $SubnetCount = ($Networks.AddressSpace.AddressPrefix).count
                if($SubnetCount -gt 0)
                    {
                        $SubNetCounts = 0
                        while($SubnetCounts -le $SubnetCount -and $SubnetCounts -ne $SubnetCount)
                        {
                            $Object = New-Object System.Object
                            $Object | Add-Member -Type NoteProperty -name "Network" -Value $Networks.Name
                            $object | Add-Member -Type NoteProperty -name "AddressPrefix" -value $Networks.AddressSpace.AddressPrefix[$SubNetCounts]
$Object | Add-Member -Type NoteProperty -name "Gateway Address" -Value $Networks.VPNGatewayAddress

                        $LocalSite += $Object
                        $SubNetCounts++
                        }
                    }
            $Count++
            }

    }

#-------------------- END

$LocalSite

}

function Get-AzureNetworkDNS
{
        #Get the current Network Config
        $XML = Get-AzureVNetConfig
         
        #Define it as XML
        [ xml ]$fileContents = $XML.XMLConfiguration
           
       $networks = $fileContents.DocumentElement.VirtualNetworkConfiguration.Dns.DnsServers.DnsServer
 
       $networks 
}

Clear

Write-Output "Azure - Virtual Networks"
Get-AzureVirtualNetwork | Format-Table -AutoSize

Write-Output "Azure - Virtual Network Routes"
Get-AzureVirtualNetworkRoutes | Format-Table -AutoSize

Write-Output "Azure - Virtual Network DNS"
Get-AzureNetworkDNS | Format-Table -AutoSize

Reference: http://blog.kloud.com.au/2014/11/11/get-azure-virtual-networks-with-powershell/

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Azure Code PowerShell Get-AzureVirtualNetworkGet-AzureVirtualNetworkRoutesIP RangeNetworkNetwork RoutesVirtual Network

Post navigation

Previous post
Next post

Related Posts

PowerShell – Clean Windows 10

Posted on March 9, 2016April 2, 2022

Work In Progress This will remove some of the default apps along with a couple other fun features to come. clear # Many of the below items are from #https://tweakhound.com/2015/12/09/tweaking-windows-10/ #cd ~\Desktop #mkdir "God Mode.{ED7BA470-8E54-465E-825C-99712043E01C}" function Change-Service ($servicename, $serviceaction, $servicestatuptype) { if (Get-Service $servicename -ErrorAction SilentlyContinue) { $ServiceDetail=$(Get-Service -Displayname $servicename)…

Read More

Check Instance Of App

Posted on December 14, 2013October 26, 2015

Imports System.Diagnostics Private Sub CheckInstanceOfApp() Dim appProc() As Process Dim strModName, strProcName As String strModName = Process.GetCurrentProcess.MainModule.ModuleName strProcName = System.IO.Path.GetFileNameWithoutExtension(strModName) appProc = Process.GetProcessesByName(strProcName) If appProc.Length > 1 Then MessageBox.Show("There is an instance of this application running.", "<Application Name>") Application.Exit() Else ‘MessageBox.Show("There are no other instances running.", "<Application Name>") End If…

Read More

PowerShell – Windows Active Directory – Compare User Groups

Posted on January 2, 2020

$user1 = "administrator" $user2 = "dkittell" # Left arrow means user1 has the permissions but user2 does not # Right arrow means user2 has the permissions but user1 does not diff(get-adprincipalgroupmembership -Identity $user1)(get-adprincipalgroupmembership -Identity $user2) -property ‘name’ All information on this site is shared with the intention to help. Before…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes