If you’ve spent any time managing firewall rules, you already know that chasing down dynamic IP addresses of modern web services is impossible. Fortunately, NSX offers layer 7 context profiles, allowing you to drop traffic based on domain name (FQDN) by inspecting the TLS SNI or the plaintext HTTP host header. It’s a great way to enforce basic web filtering.
Category: NSX
Here’s a quick script for retreiving a list of VMs with tags from NSX via PowerShell.
The key is utilizing the /api/v1/fabric/virtual-machines API method, then iterating through the results. API documentation can for this method be found here.
$nsxtManager = "https://nsx-manager.example.com"
$username = "user"
$password = "supersecret"
$response = Invoke-RestMethod -Uri "$nsxtManager/api/v1/fabric/virtual-machines" -Method Get -Headers @{
"Authorization" = "Basic $( [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${username}:${password}"))) )"
"Content-Type" = "application/json"
} -Body '{}' -SkipCertificateCheck
$output = @()
foreach ($item in $response.results) {
if ($item.tags) {
$vmTags = ""
foreach ($tag in $item.tags) {
$vmTags += "Scope: " + $tag.scope + " Tag: " + $tag.tag + " "
}
$vmData= [PSCustomObject]@{
"VM Name" = $item.display_name
Tags = $vmTags
}
$output += $vmData
}
}
$output
$output | Export-Csv -Path "/path/to/save/nsx-vm-tags.csv" -NoTypeInformation
Get it on GitHub here: https://github.com/blazcode/PowerShell/blob/main/NSX/Get%20NSX%20VM%20Tags.ps1
Here’s a quick script for retrieving a list of tags from NSX via PowerShell.
The key to the script is utilizing the /policy/api/v1/infra/tags API method, then iterating through the results. This method documentation can be found here.
$nsxtManager = "https://nsx-manager.example.com"
$username = "user"
$password = "supersecret"
$response = Invoke-RestMethod -Uri "$nsxtManager/api/v1/infra/tags" -Method Get -Headers @{
"Authorization" = "Basic $( [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${username}:${password}"))) )"
"Content-Type" = "application/json"
} -Body '{}' -SkipCertificateCheck
$output = @()
foreach ( $item in $response.results ) {
$tagData= [PSCustomObject]@{
"Tagged Objects Count" = $item.tagged_objects_count
"Scope" = $item.scope
"Tag" = $item.tag
}
$output += $tagData
}
$output
$output | Export-Csv -Path "/path/to/save/nsx-tags.csv" -NoTypeInformation
Get in on GitHub here: https://github.com/blazcode/PowerShell/blob/main/NSX/Get%20NSX%20Tags.ps1