Nutanix/ServerRake.Nutanix.Read.ps1

#################################
### NUTANIX FUNCTIONS: READ ###
#################################

function Get-NutanixVMs {

    [CmdletBinding()]
    param (

        [Parameter(Mandatory = $true)]
        [String] $PrismCentralUrl

    )

    if (!$nxAPICreds) {

        throw "No Nutanix credentials have been set. Use 'Set-NutanixCredentials -Username' to set credentails and try again."
    
    }

    $baseUrl = "https://$($PrismCentralUrl):9440/api/nutanix/v3"
    $offsetby = 0
    $count = 0
    $totalVMsFound = $null

    $Headers = @{
        "Authorization" = "Basic $nxAPICreds"
        "Content-Type"  = "application/json"
        "Accept"        = "application/json"
    }

    $response = @()
    do {

        $count    += 500

        $Body = @{
            kind   = "vm"
            offset = $offsetby
            length = $count
        } | ConvertTo-Json

        $payload = Invoke-RestMethod -Uri "$baseUrl/vms/list" -Method Post -Headers $Headers -Body $Body -ErrorAction Stop -SkipCertificateCheck

        try {

            $payload = Invoke-RestMethod -Uri "$baseUrl/vms/list" -Method Post -Headers $Headers -Body $Body -ErrorAction Stop -SkipCertificateCheck

        } catch {

            throw $_

        }

        foreach ($p in $payload.entities) {
            $response += [PSCustomObject]@{
                Server                  =   $p.spec.name
                'VM CPUs'               =   $p.spec.resources.num_sockets
                'VM Cores (per CPU)'    =   $p.spec.resources.num_vcpus_per_socket
                'VM Power State'        =   $p.spec.resources.power_state
                'VM NIC List'           =   $p.spec.resources.nic_list
                'VM Disk List'          =   $p.spec.resources.disk_list
                'VM UUID'               =   $p.metadata.uuid
                'VM Created'            =   $p.metadata.creation_time
                'VM Memory (GB)'        =   [math]::round($p.entities.spec.resources.memory_size_mib / 1GB)
            }
        }

        if (!$totalVMsFound) {$totalVMsFound = $payload.metadata.total_matches}
        $offsetby   += 500

    } while ($count -lt $totalVMsFound)

    return $response

}