Public/Get-FilteredDisconnectedSessions.ps1

<#
.SYNOPSIS
    Retrieves disconnected sessions for a specified desktop group, excluding users from specific Active Directory groups.
 
.DESCRIPTION
    This function retrieves disconnected sessions from a specified Citrix Delivery Controller for a given desktop group. It excludes users who are members of specific Active Directory groups from the session list.
 
.PARAMETER AdminAddress
    The address of the Citrix Delivery Controller server from which to retrieve session information.
 
.PARAMETER SessionStateChangeTime
    The cutoff time for session state change. Sessions with a state change earlier than this time are considered.
 
.PARAMETER DesktopGroupName
    The name of the desktop group for which to retrieve disconnected sessions.
 
.PARAMETER ExcludedADGroups
    An array of Active Directory group names whose members will be excluded from the session list.
 
.EXAMPLE
    Get-FilteredDisconnectedSessions -AdminAddress "DDC1" -SessionStateChangeTime "-0:45" -DesktopGroupName "DesktopGroup1" -ExcludedADGroups @("DL-all", "DL-allFACTS")
    Retrieves disconnected sessions for "DesktopGroup1" on "DDC1" with a session state change time before "2024-04-01", excluding users from "DL-all" and "DL-allFACTS" groups.
 
.NOTES
    Requires Citrix PowerShell SDK and administrative rights to the Citrix environment.
#>


Function Get-FilteredDisconnectedSessions {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $AdminAddress,
        
        [Parameter(Mandatory=$true, Position=1)]
        [string] $SessionStateChangeTime,
        
        [Parameter(Mandatory=$true, Position=2)]
        [string] $DesktopGroupName,
        
        [Parameter(Mandatory=$true)]
        [string[]] $ExcludedADGroups
    )

    $output = @()
    $dtformat = "\[dd/MM/yy HH:mm:ss\]"
    $dateTime = Get-Date -f $dtformat

    $UserList = @()

    foreach ($group in $ExcludedADGroups) {
        $members = Get-ADGroupMembers -GroupName $group -OutputFormat "upn"
        $UserList += $members
    }

    $Sessions = Get-BrokerSession -AdminAddress $AdminAddress -SessionState Disconnected -Filter { SessionStateChangeTime -lt $SessionStateChangeTime } -DesktopGroupName $DesktopGroupName | Select-Object -ExpandProperty UserUPN

    $List = $Sessions | Where-Object { $UserList -notcontains $_ }

    foreach ($Lists in $List) {
        $user = Get-BrokerSession -AdminAddress $AdminAddress -SessionState Disconnected -Filter { SessionStateChangeTime -lt $SessionStateChangeTime } -DesktopGroupName $DesktopGroupName -UserUPN $Lists 

         $startTime = $user.SessionStateChangeTime
         $currentTime = Get-Date
         $timeDiff = New-TimeSpan -Start $startTime -End $currentTime
         if ($timeDiff.TotalHours -ge 24) {
             $formattedTime = "{0}d:{1}h:{2:D2}m:{3:D2}s" -f $timeDiff.Days, $timeDiff.Hours, $timeDiff.Minutes, $timeDiff.Seconds
         } elseif ($timeDiff.TotalMinutes -ge 60) {
             $formattedTime = "{0}h:{1:D2}m:{2:D2}s" -f $timeDiff.Hours, $timeDiff.Minutes, $timeDiff.Seconds
         } else {
             $formattedTime = "{0}m:{1:D2}s" -f $timeDiff.Minutes, $timeDiff.Seconds
         }

        $ness     = $formattedTime
        $SSs      = $user.sessionstate
        $sns      = $user.UserName
        $mcs      = $user.HostedMachineName
        $output += "$dateTime : $username[$sns] is $SSs for $ness on $mcs"
    }

    $output
}