I have a PowerShell script that uses Invoke-RestMethod with Basic authentication to query NSX‑T: first to list all Tier‑0 gateways, then for each Tier‑0 get its locale‑services, then for each locale-service get BGP neighbor status.
Because Basic auth is specified on every request, each Invoke‑RestMethod call re‑authenticates. In a large NSX‑T environment with many Tier‑0 and locale‑services, that means dozens or hundreds of auth checks each run. For example, if I have 50 Tier‑0s and each has 4 locale-services, that's 1 + 50 + 200 = 251 HTTP requests each run - and 251 separate authentication attempts.
While the script works fine and only performs GET requests, I wonder:
-
Is this repeated authentication putting unnecessary load on NSX‑T Manager?
-
Should I switch to a "login once → reuse token → many GETs" approach (e.g. via the NSX‑T REST token endpoint) - and is this supported / recommended with NSX‑T 4.2.2.1 + PowerCLI?
-
If I change to token‑based auth, are there any known pitfalls when using PowerCLI / Invoke‑RestMethod + Bearer token for NSX‑T?
What I'm asking you:
-
Any experience from others with large NSX‑T environments + PowerCLI + REST API regarding load / performance.
-
Suggestions or sample patterns for token‑based authentication + efficient querying (especially for large number of objects).
-
Warnings or "gotchas" - e.g. token expiry, API‑rate limiting, concurrency issues.
The Code:
$nsx = "https://x"
$user1031 = "x@xx"
$1031Password = Get-Content C:\Users\Administrator\Desktop\x.txt | ConvertTo-SecureString
$credential1031 = New-Object System.Management.Automation.PSCredential ($user, $Password)
# 1) Get all Tier-0 Gateways
$tier0s = Invoke-RestMethod -Uri "$nsx/policy/api/v1/infra/tier-0s" -Method GET -SkipCertificateCheck -Headers @{Accept="application/json"} -Authentication Basic -Credential $credential
$allNeighbors = @()
foreach ($t0 in $tier0s.results) {
$t0id = $t0.id
# 2) Get locale-services for this Tier-0
$ls = Invoke-RestMethod -Uri "$nsx/policy/api/v1/infra/tier-0s/$t0id/locale-services" `
-Method GET -SkipCertificateCheck -Headers @{Accept="application/json"} -Authentication Basic -Credential $credential1031
foreach ($lsitem in $ls.results) {
$lsid = $lsitem.id
# 3) Get BGP neighbor status
$statusUrl = "$nsx/policy/api/v1/infra/tier-0s/$t0id/locale-services/$lsid/bgp/neighbors/status"
$status = Invoke-RestMethod -Uri $statusUrl -Method GET -SkipCertificateCheck -Headers @{Accept="application/json"} -Authentication Basic -Credential $credential1031
# 4) Extract only what you want (without locale service)
foreach ($nbr in $status.results) {
$allNeighbors += [PSCustomObject]@{
Tier0 = $t0.display_name
neighbor_address = $nbr.neighbor_address
connection_state = $nbr.connection_state
}
}
}
}
# Output
$allNeighbors | Where-Object { $_.connection_state -ne "ESTABLISHED" } | Format-Table -AutoSize
-------------------------------------------