Tracking network changes & troubleshooting connectivity problems usually results in analysis of route tables and in Azure currently there are a few route table constructs (Gateway, Express Route, Subnet route tables, effective route table…). The route table potentially of most importance or interest is the effective route table on a NIC attached to a VM in Azure and knowing which subnet it’s attached to. Network Security Groups are just as important with regards to what traffic they allow in and out.
Below is a script that will cycle through ALL your network interfaces across all running VMs and fetch the effective route table and also the NSG if one is attached. It will output the route table and NSG in standard Powershell with some manipulation of route table data using Format-Table format into a file named as the NIC itself. A header is placed into the files to also confirm the NIC name and advise which VM and also subnet it’s attached to in the case of VMs & NVAs with multiple network interfaces.
This script uses a combined mixture of foreach loops with some manual generation & handling of arrays and is heavily reliant on output data from the Get-AzNetworkInterface command. The output in particular of this command needed some work to manipulate to drill down into sub property values of the VM Name and Subnet attached to the NIC, I sought advice from another blog: https://4sysops.com/archives/retrieve-azure-nic-properties-using-powershell/.
The Get-AzEffectiveNetworkSecurityGroup is used to get NSG information. I couldn’t find an immediate way to dynamically assess if an NSG would apply (command output only lists an NSG that is directly attached to the NIC as opposed to inherited from Subnet) to a NIC or not to this command is run regardless but if empty no data is saved to file.
I’ll probably turn this into a function at some time to automate looping through by being called from elsewhere, some brief overview of the script below:
- Setup file output environment and file names.
- Enumerate all NICs into an array by name and Resource Group
- Search for NICs and if found
- Check for output file folder and create if required
- Otherwise advise none found
- Prepare arrays for output file names and search pattern
- Cycle through all NICs via foreach loop
- Get VM Name and Subnet NIC attached to
- Check power state of VM and if running, get route table and run compare.
- If there is an NSG attached, it will fetch that too.
- Run Comparison function
Some of the commands take a second or so to execute so the script will take time to cycle through, the slowest check is validating if the VM is running. Looks like I’ll have some more work to do to perhaps change the order of things to enumerate running VMs instead and then work on getting NIC details potentially!
The script is located at https://github.com/roity57/Azure-Gather-and-Compare-Info/blob/master/AzNICRouteTable.ps
(Updated 2/8/2020 – added NSG enumeration and all scripts now located at Github with version control)