Public/Get-CitrixSessionActivities.ps1

<#
.SYNOPSIS
    Retrieves Citrix session activities within a specified date range.
 
.DESCRIPTION
    This function retrieves Citrix session activities within a specified date range from the Citrix Monitor OData service. It returns a custom object with details such as application name, modified date, peak concurrent instance count, and deployment state.
 
.PARAMETER DeliveryController
    Specifies the hostname or IP address of the Citrix Delivery Controller.
 
.PARAMETER StartDate
    Specifies the start date of the date range for retrieving session activities.
 
.PARAMETER EndDate
    Specifies the end date of the date range for retrieving session activities.
 
.EXAMPLE
    # Retrieve Citrix session activities from the specified Delivery Controller for the month of January 2024
    $ReportLMonth = (Get-Date).AddMonths(-1).Date.AddDays(-((Get-Date).Day-1))
    $StartDate = '{0}-01T00:00:00' -f $ReportLMonth.ToString('yyyy-MM')
    $EndDate = '{0}T23:59:59' -f $ReportLMonth.AddMonths(1).AddDays(-1*$ReportLMonth.Day).ToString('yyyy-MM-dd')
    Get-CitrixSessionActivities -DeliveryController ctxddc001 -StartDate $StartDate -EndDate $EndDate
.NOTES
    Requires Citrix PowerShell SDK and appropriate administrative credentials.
#>

function Get-CitrixSessionActivities {
    [CmdletBinding()]
    [OutputType('PSCustomObject')]
    param(
        [Parameter(Mandatory=$true)]
        [string]$DeliveryController,

        [DateTime]$StartDate,

        [DateTime]$EndDate
    )

    # Define REST query helper
    $invokeRestQuery = {
        param($Uri)
        try {
            return Invoke-RestMethod -Uri $Uri -UseDefaultCredentials -ErrorAction Stop
        } catch {
            Write-Error "Failed to invoke REST query: $_"
            return $null
        }
    }

    # Fetch application activity summaries
    # Fetch application activity summaries
$appActivityUri = "http://$DeliveryController/Citrix/Monitor/OData/v3/Data/ApplicationActivitySummaries?`$format=json&`$filter=(SummaryDate ge DateTime'$($StartDate.ToString("yyyy-MM-ddTHH:mm:ss"))') and (SummaryDate le DateTime'$($EndDate.ToString("yyyy-MM-ddTHH:mm:ss"))')"

    $appActivity = & $invokeRestQuery $appActivityUri

    # Fetch applications details
    $appsUri = "http://$DeliveryController/Citrix/Monitor/OData/v3/Data/Applications?`$format=json&`$select=Id,Name,PublishedName"
    $apps = & $invokeRestQuery $appsUri

    $deployedState = if ($DeliveryController -match "VAUS") { "DR" } else { "Prod" }

    # Process application activities
    $output = foreach ($activity in $appActivity.value) {
        $appId = $activity.ApplicationId
        $appName = ($apps.value | Where-Object { $_.Id -eq $appId }).PublishedName
        $modifiedDateOnly = Get-Date $activity.ModifiedDate -Format "MM/dd/yyyy"  # Formats the date as MM/dd/yyyy

        [PSCustomObject]@{
            AppName = $appName
            ModifiedDate = $modifiedDateOnly
            PeakConcurrentInstanceCount = $activity.PeakConcurrentInstanceCount
        }
    }

    # Group by AppName to get the maximum peak
    $monthlyPeaks = $output | Group-Object -Property AppName | ForEach-Object {
        $appName = $_.Name
        $maxPeak = ($_.Group | Measure-Object -Property PeakConcurrentInstanceCount -Maximum).Maximum
        $dateForMaxPeak = ($_.Group | Where-Object { $_.PeakConcurrentInstanceCount -eq $maxPeak } | Select-Object -ExpandProperty ModifiedDate -First 1)

        [PSCustomObject]@{
            AppName = $appName
            MaxPeakConcurrent = $maxPeak
            DateForMaxPeakConcurrent = $dateForMaxPeak
            Deployed = $deployedState
        }
    }

    return $monthlyPeaks | Sort-Object -Property MaxPeakConcurrent -Descending
}