Public/Get-CitrixLicenseUsageReport.ps1

<#
.SYNOPSIS
    Retrieves detailed Citrix licensing usage information from a specified admin server.
 
.DESCRIPTION
    The Get-CitrixLicenseUsageReport function connects to a specified Citrix Administration Server to gather and report detailed licensing information based on the specified license edition. It supports filtering by license edition and can return detailed object-based information when requested.
 
.PARAMETER AdminAddress
    The address of the Citrix Administration Server from which to fetch license data.
 
.PARAMETER LicenseEdition
    The edition of the license for which details are needed. Valid options are "PLT" (Platinum), "STD" (Standard), "AVD" (Advanced), and "ENT" (Enterprise). If not specified, the function will attempt to fetch details for all available editions.
 
.PARAMETER Values
    When specified, the function returns the licensing details as a detailed custom object instead of merely logging the details to the console.
 
.EXAMPLE
    PS> Get-CitrixLicenseUsageReport -AdminAddress 'citrix.example.com' -LicenseEdition 'PLT'
 
    This command retrieves Platinum license details from the Citrix admin server at 'citrix.example.com'.
 
.EXAMPLE
    PS> Get-CitrixLicenseUsageReport -AdminAddress 'citrix.example.com' -Values
 
    This command retrieves all license details from 'citrix.example.com' and returns them as a detailed custom object.
 
.NOTES
    This function requires that the Citrix Admin SDK PowerShell snap-ins or modules be properly installed and available on the system running the script.
#>


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

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

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

    Begin {
        $dtformat = "\[dd/MM/yy HH:mm:ss\]"
        $dateTime = Get-Date -Format $dtformat
        $licenseServer = (Get-BrokerSite -AdminAddress $AdminAddress).LicenseServerName
    } 

    Process {
        Try {
            $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
                }
            }

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

            if ($Values) {
                [PSCustomObject]@{
                    Date = $dateTime
                    TotalLicenses = $totalLicenses
                    LicensesInUse = $licensesInUse
                    PercentageUsed = "{0}%" -f $percentageUsed
                    LicenseEdition = ($LicDetails.LicenseEdition | Select-Object -Unique)
                }
            }
        } Catch {
            Write-Error "An error occurred: $_"
        }
    }
}