Public/Get-VDIDesktopList.ps1

<#
.SYNOPSIS
    Retrieves detailed information about VDI desktop groups from a specified Citrix server.
     
.DESCRIPTION
    This function queries a Citrix server for VDI desktop groups. It lists details such as the delivery group name, enabled status, associated users, and desktop counts.
     
.PARAMETER ComputerName
    The Citrix server address used for admin tasks. Default is 'ctxddc001'.
 
.PARAMETER ExportToCsv
    Specifies whether to export the output to a CSV file. The default is $false.
     
.EXAMPLE
    Get-VDIDesktopList -ComputerName "ctxddc001"
    Retrieves desktop group information from the specified server.
     
.EXAMPLE
    Get-VDIDesktopList -ComputerName "ServerAddress" -ExportToCsv $true
    Lists all enabled Desktopgroups and exports the data to a CSV file.
 
.NOTES
    Requires Citrix PowerShell SDK and appropriate administrative credentials.
#>

Function Get-VDIDesktopList {

    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$ComputerName,

        [Parameter(Mandatory = $false)]
        [bool]$ExportToCsv = $false
    )

    process {
        try {
            $DeliveryGroups = Get-BrokerDesktopGroup -SessionSupport SingleSession -AdminAddress $ComputerName

            $Desktops = foreach ($DG in $DeliveryGroups) {
                $AccessPolicyRules = Get-BrokerAccessPolicyRule -DesktopGroupName $DG.Name -AllowedConnections ViaAG
                if ($AccessPolicyRules) {
                    $IncludedUsers = $AccessPolicyRules.IncludedUsers
                    $FilteredUsers = ($IncludedUsers.Name | Where-Object { $_ -notmatch 'AMERICAS\\CitrixApplicationAccess' }) -join ', '
                } else {
                    $FilteredUsers = "No access policy found"
                }

                [PSCustomObject]@{
                    HostedOn       = "VDI"
                    DeliveryGroup  = $DG.Name
                    Desktopname    = $DG.Name
                    Enabled        = $DG.Enabled
                    GroupName      = $FilteredUsers
                    TotalDesktops  = $DG.TotalDesktops
                    DesktopKind    = $DG.DesktopKind
                } | Select-Object HostedOn, DeliveryGroup, Desktopname, Enabled, GroupName, TotalDesktops, DesktopKind
            }

          if ($ExportToCsv) {
            $path = "C:\temp\CitrixDesktops_$(Get-Date -Format 'MM-dd-yyyy').csv"
            $Desktops | Export-Csv -Path $path -NoTypeInformation -Append
            Write-Host "Data exported to $path"
        }
         else {
            $Desktops
        }
        } catch {
            Write-Error "Failed to retrieve desktop groups: $_"
        }
    }
}