Private/Test-CitrixDDCConnectivity.ps1

function Test-CitrixDDCConnectivity {
    
    <#
    .SYNOPSIS
    Checks for connectivity to the Monitor Service OData API on the specified Citrix Virtual Apps and Desktops
    Delivery Controllers.
    
    .DESCRIPTION
    The Test-CitrixDDCConnectivity cmdlet is used as a private function on the citrix-odata module to test for a
    valid connection to the Monitor Service OData API on the specified Citrix Virtual Apps and Desktops Delivery
    Controllers.
    
    This cmdlet takes a required parameter: a list of Citrix Virtual Apps and Desktops Delivery Controllers.
    Without any other parameters, it will use the current user to connect to the DDCs and test if the API is
    available.
    
    It will return an array with the DDCs that responded with a valid Monitor Service OData API connection. If the
    cmdlet cannot make a valid connection to any of the specified Delivery Controllers it will end the execution of
    the PowerShell pipeline with an error.
    
    .LINK
    https://github.com/karjona/citrix-odata
    
    .PARAMETER DeliveryControllers
    Specifies a single Citrix Virtual Apps and Desktops Delivery Controller or an array of Citrix DDCs from
    different Sites to collect data from.
    
    .PARAMETER Credential
    Specifies a user account that has permission to send the request. The default is the current user. A minimum of
    read-only administrator permissions on Citrix Virtual Apps and Desktops are required to collect this data.
    
    Enter a PSCredential object, such as one generated by the Get-Credential cmdlet.
    
    .COMPONENT
    citrix-odata
    #>

    
    
    [CmdletBinding()]
    [OutputType([String[]])]
    
    param(
    [Parameter(Mandatory=$true)]
    [String[]]
    $DeliveryControllers,
    
    [Parameter()]
    [PSCredential]
    $Credential
    )
    
    process {
        $OriginalDDCs = $DeliveryControllers
        foreach ($ddc in $DeliveryControllers) {
            $WriteProgressParams = @{
                Id = 1
                Activity = "Connecting to $ddc"
                PercentComplete = $OriginalDDCs.IndexOf($ddc)/$OriginalDDCs.length*100
            }
            Write-Progress @WriteProgressParams
            try {
                $InvokeCitrixMonitorServiceQueryParams = @{
                    DeliveryController = $ddc
                    ErrorAction = 'Stop'
                }
                if ($Credential) {
                    $InvokeCitrixMonitorServiceQueryParams.Add("Credential", $Credential)
                }
                
                $Response = Invoke-CitrixMonitorServiceQuery @InvokeCitrixMonitorServiceQueryParams
                
                if (-Not $Response[0].'odata.metadata' -contains 'Citrix') {
                    throw ("The server responded with a non-valid object. "+ 
                    "Is $ddc a valid Citrix Virtual Apps and Desktops Delivery Controller? " +
                    "The server response was: $Response")
                }
            } catch {
                $ConnectionError = $_
                Write-Error "Could not connect to $ddc`: $ConnectionError"
                $DeliveryControllers = $DeliveryControllers | Where-Object -FilterScript {$_ -ne $ddc}
            } finally {
                Write-Progress -Id 1 -Activity "Connecting to $ddc" -Completed
                if (!$DeliveryControllers) {
                    throw "Could not connect to any of the specified Delivery Controllers."
                }
            }
        }
        $DeliveryControllers
    }
}