Public/Get-ListofStaticDesktops.ps1

<#
.SYNOPSIS
    Retrieves a list of static desktops from a specified Citrix DDC server matching a certain desktop group name pattern.
 
.DESCRIPTION
    This function queries a Citrix Delivery Controller for static desktops that match a specified group name pattern. It outputs information including the VDI name, associated user names, email addresses, and delivery group.
    Additionally, it can export the results to an Excel file if required.
 
.PARAMETER DDCServer
    The address of the Citrix Delivery Controller server from which to fetch desktop information.
 
.PARAMETER DesktopGroupName
    The pattern to match desktop group names against. Default is "win10-RPA*".
 
.PARAMETER ExportToExcel
    Specifies whether to export the results to an Excel file. If true, the file is saved in the current directory with a timestamped filename.
 
.EXAMPLE
    Get-ListofStaticDesktops -DDCServer "DDC1"
    Retrieves all static desktops with a desktop group name starting with "win10-RPA" from the server "DDC1".
 
.EXAMPLE
    Get-ListofStaticDesktops -DDCServer "DDC1" -DesktopGroupName "win10-Dev*" -ExportToExcel $true
    Retrieves all static desktops with a desktop group name starting with "win10-Dev" from the server "DDC1" and exports the results to an Excel file.
     
.NOTES
    Requires Citrix PowerShell SDK and appropriate administrative credentials.
#>

Function Get-ListofStaticDesktops {

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

        [Parameter(Mandatory = $false, ValueFromPipeline = $false)]
        [string]$DesktopGroupName = "win10-RPA*",

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

    process {
        try {
            $desktops = Get-BrokerMachine -AdminAddress $DDCServer -MaxRecordCount 5000 -AllocationType Static |
                        Where-Object { $_.DesktopGroupName -like $DesktopGroupName } |
                        ForEach-Object {
                            # Extract user names and email addresses, filtering out the specific entry
                            $userNameFiltered = ($_.AssociatedUserFullNames | Where-Object { $_ -notcontains 'AMERICAS\CitrixApplicationAccess' }) -join ', '
                            $emailFiltered = ($_.AssociatedUserUPNs | Where-Object { $_ -notcontains 'AMERICAS\CitrixApplicationAccess' }) -join ', '

                            [PSCustomObject]@{
                                VDI           = $_.HostedMachineName
                                UserName      = $userNameFiltered
                                EmailAddress  = $emailFiltered
                                DeliveryGroup = $_.DesktopGroupName
                            }
                        }

            if ($ExportToExcel) {
                $excelFilePath = "C:\temp\StaticDesktops_$(Get-Date -Format 'yyyyMMdd_HHmm').csv"
                $desktops | Export-Csv -Path $excelFilePath -NoTypeInformation -Append
                Write-Host "Data exported to $excelFilePath"
            } else {
                $desktops
            }
        } catch {
            Write-Error "Failed to retrieve static desktops: $_"
        }
    }
}