Public/Invoke-CitrixDeliveryGroupsApi.ps1

<#
.SYNOPSIS
    Fetches Delivery Groups from a Citrix Delivery Controller using the Monitor Service API.
 
.DESCRIPTION
    This function makes an API call to retrieve the list of all Delivery Groups from a specified Citrix Delivery Controller.
    The data is fetched using the OData v3 endpoint and returned as a collection of custom objects.
 
.PARAMETER DeliveryController
    The hostname or IP address of the Citrix Delivery Controller from which to fetch the Delivery Groups.
 
.EXAMPLE
    $deliveryGroups = Invoke-CitrixDeliveryGroupsApi -DeliveryController "deliverycontroller.example.com"
    $deliveryGroups | ConvertTo-Json
    This example fetches the Delivery Groups from the specified Delivery Controller and stores them in the variable $deliveryGroups.
 
.NOTES
    This function requires that the user running it has the appropriate permissions to access the Citrix Monitor Service API.
 
.OUTPUTS
    PSCustomObject
    Returns a collection of objects where each object represents a Delivery Group containing properties like ID and name.
 
#>

function Invoke-CitrixDeliveryGroupsApi {
    [CmdletBinding()]
    [OutputType('PSCustomObject')]
    param(
        [Parameter(Mandatory = $true)]
        [string] $DeliveryController
    )

    # Helper function to invoke REST query
    $invokeRestQuery = {
        param($Uri)
        try {
            $response = Invoke-RestMethod -Uri $Uri -UseDefaultCredentials -ContentType "application/json" -ErrorAction Stop
            $response.value  # Directly return the 'value' property of the JSON response
        } catch {
            Write-Error "Failed to invoke REST query: $($_.Exception.Message)"
            return $null
        }
    }

    # Construct URI for API request to fetch Desktop Groups
    $desktopGroupsUri = "http://$DeliveryController/Citrix/Monitor/OData/v3/Data/DesktopGroups?`$format=json"

    # Fetch and return the Desktop Groups
    & $invokeRestQuery $desktopGroupsUri
}