Skip to content

Instantly share code, notes, and snippets.

@sammcgeown
Last active April 26, 2019 15:40
Show Gist options
  • Save sammcgeown/ef522bbcf44278b51ebd49d7de2cadbe to your computer and use it in GitHub Desktop.
Save sammcgeown/ef522bbcf44278b51ebd49d7de2cadbe to your computer and use it in GitHub Desktop.
Enable or disable an NSX Edge BGP Filter using PowerNSX
function Switch-BgpFilterAction {
param(
[string]$edgeName,
[Parameter(Mandatory=$true)][string]$filterNetwork
)
if($edgeName) {
$edges = Get-NsxEdge -Name $edgeName
} else {
$edges = Get-NsxEdge
}
foreach($edge in $edges) {
Write-Host "Edge: "$edgeName
$edgeRouting = Get-NsxEdgeRouting $edge
if($edgeRouting.bgp.bgpNeighbours.bgpNeighbour -ne $null) {
for($a=0; $a -le $edgeRouting.bgp.bgpNeighbours.bgpNeighbour.count-1; $a++){
if($edgeRouting.bgp.bgpNeighbours.bgpNeighbour[$a].bgpFilters.bgpFilter -ne $null) {
for($b=0; $b -le $edgeRouting.bgp.bgpNeighbours.bgpNeighbour[$a].bgpFilters.bgpFilter.count-1; $b++) {
if($edgeRouting.bgp.bgpNeighbours.bgpNeighbour[$a].bgpFilters.bgpFilter[$b].network -match $filterNetwork) {
switch ($edgeRouting.bgp.bgpNeighbours.bgpNeighbour[$a].bgpFilters.bgpFilter[$b].action) {
"permit" {
$edgeRouting.bgp.bgpNeighbours.bgpNeighbour[$a].bgpFilters.bgpFilter[$b].action = "deny"
Write-Host "Switching 'permit' BGP filter for $filterNetwork to 'deny'"
break
}
"deny" {
$edgeRouting.bgp.bgpNeighbours.bgpNeighbour[$a].bgpFilters.bgpFilter[$b].action = "permit"
Write-Host "Switching 'deny' BGP filter for $filterNetwork to 'permit'"
break
}
}
Set-NsxEdgeRouting -EdgeRouting $edgeRouting -Confirm:$false | Out-Null
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment