Add multiple static routes to all your NSX-v edges

When you have NSX running on a vSAN stretched cluster over two datacenters, or a Cross vCenter NSX setup over multiple datacenters you probably need static routes for the networks behind the DLR to keep the routing tables alive on the physical network routers when a datacenter failover occurs. I am working at a customer where I needed to add 10 static routes to 20 different ESG’s. It’s obvious that I didn’t want to add them manually. Therefore I created a script to put in the static routes in the edges for me. This may come in handy for somebody else.

Requirements:
– Powershell (5.1)
– PowerCLI
– PowerNSX

Tested on:
ESXi/vCenter 6.5 U1
NSX-v 6.3.3

Get-Module -ListAvailable VMware* | Import-Module
Import-Module -Name PowerNSX

$vcenterserver = “vcenter.homelab.local”
$vcenteradmin = “administrator@vsphere.local”
$vcenteradminpw = “*********”

##Networks to add as static route##

$networks = ‘192.168.41.0/24’, ‘192.168.42.0/23’, ‘192.168.44.0/22’, ‘192.168.48.0/20’, ‘192.168.64.0/21’, ‘192.168.72.0/24’, ‘192.168.75.0/24’, ‘192.168.76.0/22’, ‘192.168.80.0/20’, ‘192.168.96.0/22’
$nexthop = “192.168.20.251”

##Connect to vCenter and NSX Manager##

write-host Connecting to vCenter Server instance $vcenterserver -ForegroundColor Yellow
Connect-VIServer $vcenterserver -User $vcenteradmin -Password $vcenteradminpw -Force
Connect-NsxServer -vcenterserver $vcenterserver -Username $vcenteradmin -Password $vcenteradminpw

##Delete current static routes and add new ones##

$nsxedges = (Get-NsxEdge).Name
foreach ($edge in $nsxedges)
{
Get-nsxedge -name $edge | Get-NSXedgeRouting | Get-NsxEdgeStaticRoute | Remove-NsxEdgeStaticRoute -Confirm:$false
}

foreach ($network in $networks)
{
foreach ($edge in $nsxedges)
{
Get-NsxEdge -name $edge | Get-NsxEdgeRouting | New-NsxEdgeStaticRoute -vnic 2 -Network $network -NextHop $nexthop -MTU 9000 -AdminDistance 1 -Confirm:$false
}
}
Disconnect-VIServer -Server * -Force -Confirm:$false

If you have any questions or remarks, please reach out to me.

sources: VMware.com

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s