Categories
NSX VMware

Quick Script: Get list of NSX tags via PowerShell

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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