Public/Get-CitrixDetails.ps1

<#
.SYNOPSIS
    Retrieves detailed information about Citrix machine catalogs and associated resources and optionally exports the data to an Excel file.
 
.DESCRIPTION
    This function fetches details for each machine catalog within a Citrix environment including delivery groups,
    application counts, and provisioning details. It can also export the results to an Excel file if required.
 
.PARAMETER AdminAddress
    Specifies the administration address of the Citrix environment.
 
.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-CitrixDetails -AdminAddress "youradminaddress"
    Retrieves Citrix environment details using the specified admin address.
 
.EXAMPLE
    Get-CitrixDetails -AdminAddress "youradminaddress" -ExportToExcel $true
    Retrieves Citrix environment details and exports them to an Excel file.
     
.NOTES
    This script requires that Citrix PowerShell snap-ins be available and loaded externally if not handled within the script.
#>

Function Get-CitrixDetails {

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

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

    process {
       # Check if the Citrix snap-in is already loaded; only add if it's not
        if (-not (Get-PSSnapin -Name Citrix* -ErrorAction SilentlyContinue)) {
            Add-PSSnapin Citrix* -ErrorAction SilentlyContinue
        }

        $details = @()

        Get-BrokerCatalog -AdminAddress $AdminAddress | ForEach-Object {
            $Catalog = $_

            try {
                $DG = Get-BrokerDesktop -AdminAddress $AdminAddress -CatalogName $Catalog.Name -MaxRecordCount 2500
                $ACC = Get-AcctIdentityPool -AdminAddress $AdminAddress -IdentityPoolName $Catalog.Name
                $prov = Get-ProvScheme -AdminAddress $AdminAddress -ProvisioningSchemeName $Catalog.Name | 
                        Select-Object MasterImageVM, MemoryMB, CpuCount

                $snapshot = ($prov.MasterImageVM -split "\\")[3].split(".")[0]
                $MIName = $snapshot.Substring(0, $snapshot.IndexOf('TMP')) + "TMP"
                $tags = if ($DG.Tags -eq $null) { "N/A" } else { ($DG.Tags | Select-Object -Unique) -join ', ' }
                
                $appcount = if ($Catalog.Name -match "VDURCX*") {
                    $tmpvalue = Get-BrokerDesktopGroup -AdminAddress $AdminAddress -Name $Catalog.Name
                    (Get-BrokerApplication -AdminAddress $AdminAddress -DesktopGroupUid $tmpvalue.Uid -Filter {Enabled -eq 'True'} |
                     Select-Object -ExpandProperty ApplicationName).Count
                } else { "N/A" }

                $details += [PSCustomObject]@{
                    MachineCatalog    = $Catalog.Name
                    DeliveryGroup     = ($DG.DesktopGroupName | Select-Object -Unique) -join ', '
                    AllocationType    = $Catalog.AllocationType
                    SessionSupport    = $Catalog.SessionSupport
                    PersistUserChanges= $Catalog.PersistUserChanges
                    Tags              = $tags
                    TotalMachines     = $Catalog.usedcount
                    AppsPublished     = $appcount
                    MasterImage       = $MIName
                    SnapshotApplied   = $snapshot
                    MemoryinMB        = $prov.MemoryMB
                    OU                = $ACC.OU
                    NamingScheme      = $ACC.NamingScheme
                }
            } catch {
                Write-Error "Failed to process catalog '$($Catalog.Name)': $_"
            }
        }

        if ($ExportToExcel) {
            $filename = "C:\temp\CitrixDetails_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
            $details | Export-Csv -Path $filename -NoTypeInformation -Append
            Write-Host "Data exported to $filename"
        } else {
            $details
        }
    }
}