Public/Get-CitrixHypervisorStatus.ps1

<#
.SYNOPSIS
Retrieves hypervisor health and lifecycle status information from a Citrix Delivery Controller using the OData API.
 
.DESCRIPTION
The Get-CitrixHypervisorStatus function queries the Citrix Monitor OData API exposed by a Delivery Controller to collect details about hypervisors, such as name, lifecycle state, creation and modification dates. It uses REST API endpoints and supports both HTTP and HTTPS connections.
 
.PARAMETER DeliveryController
The hostname or FQDN of the Citrix Delivery Controller from which to fetch hypervisor information.
 
.PARAMETER Scheme
Specifies whether to connect using "http" (default) or "https". Useful when your environment requires secure API access.
 
.EXAMPLE
# Query hypervisors using default HTTP scheme
Get-CitrixHypervisorStatus -DeliveryController "ddc001"
 
.EXAMPLE
# Query hypervisors using HTTPS scheme
Get-CitrixHypervisorStatus -DeliveryController "ddc001" -Scheme "https"
 
.NOTES
The function uses `Invoke-RestMethod` with default credentials. Ensure the executing user has appropriate permissions and access to the Citrix OData API.
#>



function Get-CitrixHypervisorStatus {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$DeliveryController,

        [Parameter()]
        [ValidateSet("http", "https")]
        [string]$Scheme = "http"
    )

    try {
        # Validate DeliveryController hostname
        if (-not ([System.Net.Dns]::GetHostAddresses($DeliveryController))) {
            Write-LogEntry -Message "Cannot resolve DeliveryController hostname: $DeliveryController"
            return
        }

        # Construct a valid URI
        $uri = "${Scheme}://${DeliveryController}/Citrix/Monitor/OData/v3/Data/Hypervisors?`$format=json"


        $deployedState = if ($DeliveryController -match "VAUS") { "DR" } else { "Prod" }

        $response = Invoke-RestMethod -Uri $uri -UseDefaultCredentials -ErrorAction Stop

        if (-not $response.value) {
            Write-LogEntry -Message "No hypervisor data returned from $DeliveryController"
            return
        }

        $response.value | Sort-Object Name | ForEach-Object {
            [PSCustomObject]@{
                Name           = $_.Name
                LifecycleState = $_.LifecycleState
                CreatedDate    = (Get-Date $_.CreatedDate -Format "MM/dd/yyyy")
                ModifiedDate   = (Get-Date $_.ModifiedDate -Format "MM/dd/yyyy")
                Deployed       = $deployedState
            }
        }
    } catch {
        Write-LogEntry -Message "Failed to retrieve hypervisor details from '$DeliveryController': $_"
    }
}