PowerShell script exchange
Post Reply
matteu
Veeam Legend
Posts: 1070
Liked: 176 times
Joined: May 11, 2018 8:42 am
Contact:

Nutanix Get Worker configuration Rest API

Post by matteu »

Hello,

I would like to be able to easly export the configuration of the Nutanix AHV workers.
I create this script and it works fine :

Code: Select all


$VBRServer = "10.254.250.100"


# ============================================================
# CONFIGURATION API
# ============================================================

$VBRPort = 9419

$VBRApiVersion = "1.3-rev1"

# UUID de l'extension Nutanix AHV Veeam
$NutanixExtensionId = "799a5a3e-ae1e-4eaf-86eb-8a9acc2670e2"
$AuthUrl = "https://${VBRServer}:${VBRPort}/api/oauth2/token"
$NutanixApiBase = "https://${VBRServer}/extension/$NutanixExtensionId/api/v10"

# ============================================================
# CERTIFICATS
# ============================================================

if ($IgnoreCertificate) {

    if ($PSVersionTable.PSVersion.Major -ge 7) {

        $script:InvokeRestParams = @{
            SkipCertificateCheck = $true
        }

    }
    else {
        Write-Warning "PowerShell 5.x détecté : désactivation de la validation TLS."

        Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class TrustAllCertsPolicy : ICertificatePolicy
{
    public bool CheckValidationResult(
        ServicePoint srvPoint,
        X509Certificate certificate,
        WebRequest request,
        int certificateProblem)
    {
        return true;
    }
}
"@

        [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
        $script:InvokeRestParams = @{}
    }
}
else {

    $script:InvokeRestParams = @{}
}

# ============================================================
# AUTHENTICATION
# ============================================================

Write-Host ""
Write-Host "Connexion to Veeam Backup and Replication..." -ForegroundColor Cyan

$Credential = Get-Credential -Message "Veeam account"

$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)

try {

    $Password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($BSTR)

}
finally {
    [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
}

$AuthBody = @{

    grant_type = "password"
    username = $Credential.UserName
    password = $Password
}

$AuthHeaders = @{
    "x-api-version" = $VBRApiVersion
}

try {

    $TokenResponse = Invoke-RestMethod `
        @script:InvokeRestParams `
        -Uri $AuthUrl `
        -Method POST `
        -Headers $AuthHeaders `
        -Body $AuthBody `
        -ContentType "application/x-www-form-urlencoded"

}
catch {
    throw "Unable to authenticate to VBR : $($_.Exception.Message)"
}

$AccessToken = $TokenResponse.access_token

if (-not $AccessToken) {

    throw "No token received."
}

Write-Host "Authentification réussie." -ForegroundColor Green

# ============================================================
# HEADERS API NUTANIX
# ============================================================

$Headers = @{
    Authorization = "Bearer $AccessToken"
    Accept = "application/json"
}



# ============================================================
# PROCESS
# ============================================================


$Uri = "$NutanixApiBase/workers?limit=-1"
$Workers = Invoke-RestMethod -Uri $Uri -Method GET -Headers $Headers


$WorkerConfig = foreach ($Worker in $Workers.results) {

    $Config = $Worker.configuration

    foreach ($Network in $Config.networkSettings.networks) {

        [PSCustomObject]@{
            WorkerName            = $Config.name

            ClusterName           = $Config.clusterName
            StorageContainerId    = $Config.storageContainerId

            MaxConcurrentTasks    = $Config.maxConcurrentTasks
            CPU                   = $Config.cpuCount
            RAMGB                 = $Config.memoryGb

            HostAffinityEnabled   = $Config.affinitySettings.enabled
            AffinityHostId        = $Config.affinitySettings.hostId

            DNSAutomatic          = $Config.networkSettings.obtainDnsAutomatically
            DNSServers            = ($Config.networkSettings.dnsServers -join ", ")

            NetworkId             = $Network.networkId
            NetworkDescription    = $Network.description

            IPAutomatic           = $Network.obtainIpAutomatically
            IPAddress             = $Network.ipAddress
            SubnetMask            = $Network.subnetMask
            DefaultGateway        = $Network.defaultGateway

            Enabled               = $Worker.enabled
            Status                = $Worker.status
        }
    }
}

$WorkerConfig | Format-Table -AutoSize
Why the REST API show the clustername, the workername, but the StorageContainer and Host (when using host affinity) and network show only the ID ?
For some, there is a "name" property but the value is unknown like for network : (network ID: xxxxxxx-xxxxx-xxxxxx-xxxxx-xxxxxxxxxxx) where xxxx is the network ID

Thanks for your answer.

I know I can use RestAPI to retrieve the name from the ID, but I just try to understand why it's displayed correctly for some and not for others ?
It's maybe an improvment to make :)

Thanks for the answer.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests