Public/Initiate-SessionLogoffbyDG.ps1

<#
.SYNOPSIS
    Automatically logs off disconnected sessions based on the type of machine (Windows Server or Windows 10) in a specified delivery group.
 
.DESCRIPTION
    This function automatically logs off disconnected sessions based on the type of machine (Windows Server or Windows 10) in a specified Citrix delivery group.
    It retrieves disconnected session details, identifies the type of machine, and logs off the session accordingly.
 
.PARAMETER AdminAddress
    The address of the Citrix Delivery Controller server from which to retrieve session information.
 
.PARAMETER SessionStateChangeTime
    The cutoff time for session disconnection. Sessions disconnected before this time are considered.
 
.PARAMETER DesktopGroupName
    The name of the Citrix delivery 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
    Initiate-SessionLogoffbyDG -AdminAddress "ddcname" -SessionStateChangeTime "-0:45" -DesktopGroupName "Win10-Prod" -ExcludedADGroups @("DL-all", "DL-allFACTS")
    Automatically logs off disconnected sessions in the "Win10-Prod" delivery group on "ddcname" server within the last 45 minutes, excluding users from "DL-all" and "DL-allFACTS" groups.
 
#>

Function Initiate-SessionLogoffbyDG {
    [CmdletBinding()]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $AdminAddress,
        
        [Parameter(Mandatory=$true)]
        [string] $SessionStateChangeTime,
        
        [Parameter(Mandatory=$true)]
        [string] $DesktopGroupName,
        
        [Parameter(Mandatory=$true)]
        [string[]] $ExcludedADGroups
    )

    $SessionDetails = Get-DisconnectedSessionDetails -AdminAddress $AdminAddress -SessionStateChangeTime $SessionStateChangeTime -DesktopGroupName $DesktopGroupName -ExcludedADGroups $ExcludedADGroups

    foreach ($SessionDetail in $SessionDetails) {
        $computer = $SessionDetail.Machine
        $user = $SessionDetail.User

        # Get operating system information
        $osInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
        $isServerOS = $osInfo.ProductType -ne 1

        # Determine the session logoff action based on the operating system type
        if ($isServerOS) {
            Invoke-RemoteUserLogoff -ComputerName $computer -UserName $user
        } else {
            Stop-SessionLogoff -ComputerName $computer -AdminAddress $AdminAddress
        }
    }
}