Visualising Network Peerings with PsGraph

While discussing the merits of Azure Virtual Network Peering with colleages at work, it became apparent that I’d need to prove that peering relationships would be easy to visualise quickly and not some impossible feat making them impossible to manage at scale.

I am comfortable with PowerShell and the Az modules but would need something to actually do the visualiation, I’d already heard of PsGraph so decided to give it a go.

PsGraph

PsGraph is a PowerShell module created and maintained by Kevin Marquette, rather than re-explain what is is, here is the relevant detail from the project’s readme.

PSGraph is a helper module implemented as a DSL (Domain Specific Language) for generating GraphViz graphs. The goal is to make it easier to generate graphs using Powershell. The DSL adds these commands that are explained below.

https://github.com/KevinMarquette/PSGraph – Kevin Marquette

Looking through the project’s readme I could quite quickly see what I’d need to do to use GraphViz, other than installing it of course!

The concept is simple, you have ‘nodes’ which are the things you are drawing and ‘edges’ the lines between them. There are other concepts but for my use case I only needed “boxes and lines”.

I’ll create each network as a node and each peering as an edge. Simple

The Process

So; at a high level the process would need to be:

  • Get all Azure Virtual Networks
  • Grab the remote network name for each peering
  • Create a node for each network
  • Create an edge for each peering

Seemed simple enough.

The Code

I carried out a very quick PoC to build some virtual networks, create the peerings, and then visualise the result. I won’t write up the full script as it isn’t worth it and is available on my GitHub should you wish to read it.

The part that is important, as always, is at the bottom.

$networkMap = foreach($network in $deployedNetworks){
    $props = @{
        network  = $network.Name
        peerings = foreach($peering in $network.VirtualNetworkPeerings){
            ($deployedNetworks | ? {
                $_.Id -eq $peering.RemoteVirtualNetwork.Id
            }).Name
        }
    }
    New-Object -TypeName PsObject -Property $props
}

graph g {
    $networkMap.Network | % {node $_}
    $networkMap | % {
        $localNetwork = $_.Network
        $_.Peerings | % {
            edge $localNetwork $_
        }
    }
}| Export-PSGraph -ShowGraph

The output of which was:

GraphViz output generated by PsGraph

Closing

Kevin has gone a really good job with this module, it works and the docs are easy enough to follow. I’m going to be interested to see what else I can do with it; the first thing would be to change the icon used to to make non-peeered networks clearer.

Something that looking at the docs will take about 1 minute to complete.

Share

Let me know what you think in the comments.