Public/Get-CitrixLicenseReport.ps1

<#
.SYNOPSIS
    Retrieves Citrix License Usage and optionally exports to CSV.
 
.DESCRIPTION
    The `Get-CitrixLicenseReport` function fetches Citrix license usage details
    and can either display them in the console or save them to a CSV file.
 
.PARAMETER AdminAddress
    The Citrix Broker Admin Address.
 
.PARAMETER LicenseEdition
    The Citrix License Edition to filter (PLT, STD, AVD, ENT). Default: "PLT".
 
.PARAMETER Folder
    The folder where the CSV file should be created (if -Csv is used).
 
.PARAMETER Csv
    Saves the output to a CSV file instead of displaying values.
 
.PARAMETER Values
    Displays the report values on the console.
 
.EXAMPLE
    Get-CitrixLicenseReport -AdminAddress "BrokerServer" -Values
 
.EXAMPLE
    Get-CitrixLicenseReport -AdminAddress "BrokerServer" -Csv -Folder "C:\CitrixReports"
 
 
#>

Function Get-CitrixLicenseReport {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string] $AdminAddress,

        [Parameter(Mandatory = $false)]
        [ValidateSet("PLT", "STD", "AVD", "ENT")]
        [string] $LicenseEdition = "PLT",

        [Parameter(Mandatory = $false)]
        [string] $Folder,

        [Alias('V')]
        [switch] $Values,

        [Alias('C')]
        [switch] $Csv
    )

    Begin {
        # Ensure at least one mode is selected
        if (-not $Values -and -not $Csv) {
            Write-Host "[ERROR] Please specify either -Values or -Csv mode." -ForegroundColor Red
            return
        }

        # Validate folder if CSV mode is selected
        if ($Csv -and -not $Folder) {
            Write-Host "[ERROR] Please provide a folder path using -Folder when using -Csv." -ForegroundColor Red
            return
        }

        if ($Csv -and -not (Test-Path -Path $Folder)) {
            Write-Host "[INFO] Creating output folder: $Folder" -ForegroundColor Yellow
            New-Item -Path $Folder -ItemType Directory -Force | Out-Null
        }

        # Set up Date Formatting
        $YDate = Get-Date -Format "yyyy-MM-dd"
        $dateTime = Get-Date -Format "dd/MM/yy HH:mm:ss"

        # Generate CSV file path
        if ($Csv) {
            $CsvFolder = Join-Path -Path $Folder -ChildPath "Daily\csv"
            $CsvFilePath = Join-Path -Path $CsvFolder -ChildPath "licreport_$YDate.csv"
        }

        # Get License Server
        $licenseServer = (Get-BrokerSite -AdminAddress $AdminAddress).LicenseServerName
    } 

    Process {
        Try {
            # Retrieve license details
            $cert = Get-LicCertificate -AdminAddress "https://$($licenseServer):8083"
            $ctxlic = Get-LicInventory -AdminAddress "https://$($licenseServer):8083" -CertHash $cert.CertHash |
                      Where-Object { $_.LicenseEdition -eq $LicenseEdition }

            $LicDetails = foreach ($lic in $ctxlic) {
                [PSCustomObject]@{
                    LicenseProductName                  = $lic.LocalizedLicenseProductName
                    LicenseModel                        = $lic.LocalizedLicenseModel
                    LicensesInstalled                   = $lic.LicensesAvailable
                    LicensesInUse                       = $lic.LicensesInUse
                    LicensesAvailable                   = $lic.LicensesAvailable - $lic.LicensesInUse
                    LicenseSubscriptionAdvantageDate    = $lic.LicenseSubscriptionAdvantageDate
                    LicenseEdition                      = $lic.LicenseEdition
                }
            }

            # Summarized Report Data
            $totalLicenses = ($LicDetails | Measure-Object -Property LicensesInstalled -Sum).Sum
            $licensesInUse = ($LicDetails | Measure-Object -Property LicensesInUse -Sum).Sum
            $percentageUsed = [Math]::Round($licensesInUse / $totalLicenses * 100, 2)

            # Create Report Object
            $Report = [PSCustomObject]@{
                Date           = $dateTime
                TotalLicenses  = $totalLicenses
                LicensesInUse  = $licensesInUse
                PercentageUsed = "{0}%" -f $percentageUsed
                LicenseEdition = ($LicDetails.LicenseEdition | Select-Object -Unique)
            }

            # Display Values in Console
            if ($Values) {
                Write-Host "`n[INFO] License Report for $LicenseEdition Edition:" -ForegroundColor Cyan
                $Report | Format-Table -AutoSize
            }

            # Append to CSV
            if ($Csv) {
                $Report | Export-Csv -Path $csvFilePath -Append -NoTypeInformation
                Write-Host "`n[SUCCESS] License report appended to: $csvFilePath" -ForegroundColor Green
            }

        } Catch {
            Write-Host "[ERROR] An error occurred: $_" -ForegroundColor Red
        }
    }
}