Powershell report on NSX IP Pools

While troubleshooting and NSX install I began wondering about the NSX IP pools and what was being used in them.  I still need to look up the allocated IPs and report on the objects using them (name, type) but for now the (very basic) report spits back IP pool name, the configured ranges, the total IPs in the ranges, the number of IPs used and which specific IPs are used.

Pool name: VTEP Pool
Configured range(s): 192.168.89.100 - 192.168.89.200
Total IPs: 101
Number of IPs used: 4
IPs used: 192.168.89.100, 192.168.89.101, 192.168.89.102, 192.168.89.103

Note when you go to configure pools, if you want to allocate disjointed ranges you need to separate them with commas – this is no longer shows in the UI with 6.3. Also, to add single IPs just make a range out of them like: 192.168.2.50-192.168.2.50, 192.168.2.52-192.168.2.52, 192.168.2.55-192.168.2.55.

The script leverages a shorter version of the NSXAPI function I stole from Chris Wahl a while back.

I’ve only tested with Powershell 5.0 – the original NSXAPI function created a new type which errors in PS5:

add-type : Cannot add type. The type name ‘TrustAllCertsPolicy’ already exists.

so I chopped that part of the code out along with most comments.

I’ll update this post when I tweak the script to report fully on the IPs allocated.

function NSXAPI {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
[String]$Request
)
Process {
$Username = "admin"
$Password = "vmware"
$NSXManager = "nsxmanager.corp.local"

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Username + ":" + $Password))
$head = @{"Authorization"="Basic $auth"}

$r = Invoke-WebRequest -Uri $Request -Headers $head -ContentType "application/xml" -ErrorAction:Stop
[xml]$rxml = $r.Content
$rxml
} # End of process
} # End of function

foreach ($pool in $pools) {
$name = $pool.name
$universal = $pool.isUniversal
$total = $pool.totaladdresscount
$used = $pool.usedaddresscount
if (($pool.ipranges.iprangedto).count) {
$range = ""
foreach ($r in $pool.ipranges.iprangedto) {
if ($range -eq "") {
$range = $range + "$($r.startaddress) - $($r.endaddress)"
} else {
$range = $range + ", $($r.startaddress) - $($r.endaddress)"
}
}
} Else {
$range = "$($pool.ipranges.iprangedto.startaddress) - $($pool.ipranges.iprangedto.endaddress)"
}
$string= "https://nsxmanager.corp.local/api/2.0/services/ipam/pools/$($pool.objectid)/ipaddresses"
$allocated = (NSXAPI $string).allocatedIpAddresses.allocatedIpAddress
$ips = ""
foreach ($a in $allocated) {
if ($ips) {
$ips= $ips + ", $($a.ipaddress)"
} else {
$ips= "$($a.ipaddress)"
}

}
"Pool name: $name"
"Configured range(s): $range"
"Total IPs: $total"
"Number of IPs used: $used"
"IPs used: $ips"
" "
" "
}

This entry was posted in Network, NSX, PowerShell, Scripting, Virtualization, VMware and tagged , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.