Script Sample - Disassociate and Associate Subnets to Route Tables@20aman Jan 07, 2019
If you need to test connectivity of your environment without the User Defined Routes then the easiest and most non-disruptive way is to simply disassociate the subnets from the Route Tables. Once your testing is complete, then you can simply reassociate the subnets back to the original Route Tables.
Note: Use these scripts with caution as these will affect the networking and communication between resources. Go through the scripts and ensure that you know what the scripts are doing before executing these in your environment. Also, always test these in a dev environment before running in any other environment.
Script samples and how they work
The scripts in this sample are:
- Disassociate-SubnetsFromUDRs.ps1 - This script disassociates the subnets from the Route Tables
- Associate-SubnetsToUDRs.ps1 - This script reassociates the subnets back to the Route Tables. This uses a CSV file as an input. A sample of the CSV file is also provided along with the script.
The disassociation script fetches the Route Tables and then gets the associated Subnet details from there. It then removes the association by setting the route table property on the subnet to $null as shown below:
Set-AzureRmVirtualNetworkSubnetConfig ` -Name $subnetName ` -VirtualNetwork $virtualNetwork ` -AddressPrefix $subnetAddressPrefix ` -RouteTable $null | Set-AzureRmVirtualNetwork
The association script works in an exactly opposite way. The major difference is that this script fetches the information from a CSV file as provided along with the sample. This can also be autogenerated (prior to disassociation). The script sample for generating this CSV can be found here: Generate Report for Route Tables with associated Subnets and related information
The command for creating the association of Subnet to the Route Table looks like below.
Set-AzureRmVirtualNetworkSubnetConfig ` -Name $subnetName ` -VirtualNetwork $virtualNetwork ` -AddressPrefix $subnetAddressPrefix ` -RouteTable $routeTable | Set-AzureRmVirtualNetwork
Location of the Scripts
You can find this script in GitHub at this location: User Defined Routes (UDRs) or Route Tables Related Scripts