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.

– 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 = ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’
$nexthop = “”

##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.




Leave a Reply

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

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

Facebook photo

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

Connecting to %s