Categories
NSX VMware

Quick Script: Get list of VMs with tags from NSX via PowerShell

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

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

Categories
VMware

Quick tip: SDDC Manager stuck at “VMware Cloud Foundation is initializing…”

I ran into an issue with SDDC Manager getting stuck at “VMware Cloud Foundation is initializing…” today. I rebooted and restarted services to no avail. I checked the log:

/var/log/vmware/vcf/operationsmanager/operationsmanager.log 

I was immediately greeted with DNS errors:

java.net.UnknownHostException: vcf-mgmt1-vc1.lab.blaz.tech: Temporary failure in name resolution

So, no life-change master tricks here, but a friendly reminder that it’s always DNS. My docker container went boom in the night and I hadn’t yet configured backup DNS in SDDC Manager.