Trying to set up a script to enable or disable all of the static routes in a network - we are currently in a transitional period where if our MPLS fails, we need to disable (but not delete) the static routes, and then reenable them once the link comes back up.
My issue is that it takes FOREVER, i.e. multiple seconds, for each update static route (PUT) to return in my powershell script before the next one proceeds.
I've tried putting the API call into {}'s and using Start-Job, but that makes it do nothing.
I am aware of the 5/sec rate limit, if I can even get this to run at 2 or 3/sec I would be happy.
It does run and work, it just takes longer than doing it by hand. Some of the API calls take multiple seconds to return.
Here is my scrubbed script below, if anyone can help me out I would really appreciate it.
** Updated code to streamline some things I didn't need. End result is still the same right now though.
*** Added some input validation, no changes to the functional parts of the code.
#below line fixes TLS negotiation failure
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#insert API Key Below
$APIKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
$base = 'https://n158.meraki.com/api/v0'
$url = '/organizations'
$uri = $base + $url
$header = @{
'X-Cisco-Meraki-API-Key' = "$APIKey"
'Content-Type' = 'application/json'
}
$Organizations = Invoke-RestMethod -Method Get -Uri $uri -Headers $header
foreach ($Organization in $Organizations) {
$tempstr = $Organization.Name
Write-Output "Getting List of Networks from Organization $tempstr"
}
$tempstr = $obj.ID
$url = "/organizations/$tempstr/networks"
$uri = $base + $url
Start-Sleep -Milliseconds 200
$networks = Invoke-RestMethod -Method GET -uri $uri -Headers $header
Write-Output "Networks found:"
foreach ( $item in $networks ) {
Write-Output $item.name
}
$netname = Read-Host -Prompt 'Type the name of the network you wish to enable/disable static routes on'
Write-Output "You have selected network $netname"
#incorrectnet checks for bad network name
$incorrectnet = 1
foreach ( $item in $networks ) {
if($netname -eq $item.name){
$selectednet = $item
$incorrectnet = 0
}
}
if($incorrectnet -eq 1){
Write-Host $netname "is not a valid network name, please try again"
exit
}
$conditional = 3
$conditional = Read-Host -Prompt "Press 1 to enable all static routes, 2 to disable all static routes"
#outputing choice to console
If ($conditional -eq 1){
Write-Output "Enabling all static routes on $netname"
$enable = 'true'
}
Elseif ($conditional -eq 2){
Write-Output "Disabling all static routes on $netname"
$enable = 'false'
}
Else {Write-Output 'Please try again and select a valid option'
exit
}
#select network ID of selected network for use in next API call
$netid = $selectednet.id
$url = '/networks/' + $netid
$uri = $base + $url + '/staticRoutes/'
Start-Sleep -Milliseconds 200
$staticroutes = Invoke-RestMethod -Method GET -Uri $uri -Headers $header
foreach ($item in $staticroutes ) {
$body = @{
"name" = $item.name
"subnet" = $item.subnet
"gatewayIp" = $item.gatewayIp
"enabled" = $enable
}
$jsonbody = ConvertTo-Json -Inputobject $body
$url = '/networks/' + $netid + '/staticRoutes/' + $item.id
$uri = $base + $url
#blackhole variable is to prevent restmethod from outputting to console
#Sleep is to limit api calls to 5/sec
Start-Sleep -Milliseconds 200
Write-host "Setting Route" $item.name "to" $enable
$blackhole = Invoke-RestMethod -Method PUT -uri $uri -Headers $header -Body $jsonbody -UseBasicParsing
}
Write-host "Completed!"