Public/Write-CitrixSessionLog.ps1

<#
.SYNOPSIS
    Writes the HTML header for a service status report.
 
.DESCRIPTION
    This function writes the HTML header for a service status report. It includes styling, title, and introductory message.
 
.PARAMETER fileName
    Specifies the path of the file to which the HTML header will be written.
 
.EXAMPLE
    writeHtmlHeader -fileName "C:\Path\To\Report.html"
    Writes the HTML header to the specified file path.
 
.NOTES
    Author: Sundeep Eswarawaka
#>

Function writeHtmlHeader
{ 
   param($fileName) 
   $CurrentDate=(Get-Date -UFormat "%A, %d. %B %Y %R")
    Add-Content $fileName "<html>" 
    Add-Content $fileName "<head>" 
    Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>" 
    Add-Content $fileName '<title>Service Status Report </title>' 
    add-content $fileName '<STYLE TYPE="text/css">' 
    add-content $fileName  "<!--" 
    add-content $fileName  "td {" 
    add-content $fileName  "font-family: Tahoma;" 
    add-content $fileName  "font-size: 11px;" 
    add-content $fileName  "border-top: 1px solid #999999;" 
    add-content $fileName  "border-right: 1px solid #999999;" 
    add-content $fileName  "border-bottom: 1px solid #999999;" 
    add-content $fileName  "border-left: 1px solid #999999;" 
    add-content $fileName  "padding-top: 0px;" 
    add-content $fileName  "padding-right: 0px;" 
    add-content $fileName  "padding-bottom: 0px;" 
    add-content $fileName  "padding-left: 0px;" 
    add-content $fileName  "}" 
    add-content $fileName  "body {" 
    add-content $fileName  "margin-left: 5px;" 
    add-content $fileName  "margin-top: 5px;" 
    add-content $fileName  "margin-right: 0px;" 
    add-content $fileName  "margin-bottom: 10px;" 
    add-content $fileName  "" 
    add-content $fileName  "table {" 
    add-content $fileName  "border: thin solid #000000;" 
    add-content $fileName  "}" 
    add-content $fileName  "-->" 
    add-content $fileName  "</style>" 
    Add-Content $fileName "</head>" 
    Add-Content $fileName "<body>" 
    Add-Content $fileName "Hi Team,<br />"
    Add-Content $fileName "<br />"
    Add-Content $fileName "The Disconnected Session Logoff is now automated , you will only see the list of sessions below.<br />"
    Add-Content $fileName "<br />"
    Add-Content $fileName "The below sessions will get Logoff automatically.<br />"
    Add-Content $fileName "<br />"
    add-content $fileName  "<table width='100%'>" 
    add-content $fileName  "<tr bgcolor='#CCCCCC'>" 
    add-content $fileName  "<td colspan='4' height='25' align='center'>" 
    add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>Citrix Disconnected Sessions Status Alert - $CurrentDate EST</strong></font>" 
    add-content $fileName  "</td>" 
    add-content $fileName  "</tr>" 
    add-content $fileName  "</table>" 
} 

Function writeTableHeader
{ 
   param($fileName) 
   Add-Content $fileName "<tr bgcolor=#CCCCCC>" 
   Add-Content $fileName "<td width='20%' align='center'><font face='tahoma' color='#003399' size='2'><strong>Delivery Group Name</strong></font></td>" 
   Add-Content $fileName "<td width='80%' align='center'><font face='tahoma' color='#003399' size='2'><strong>Disconnected status</strong></font></td>" 
   Add-Content $fileName "</tr>" 
}

Function writeHtmlFooter
{ 
   param($fileName) 
   Add-Content $fileName "</body>" 
   Add-Content $fileName "</html>" 
} 

Function writeDiskInfo 
{ 
  param($filename,$name,$Status) 
  increment $global:a 
  Add-Content $fileName "<tr>" 
  Add-Content $fileName "<td bgcolor='#FFFAFA' align=left ><b>$name</td>" 
  Add-Content $fileName "<td bgcolor='#FFFAFA' align=left ><b><font face='tahoma' color='#FF0000'>$Status</font></td>" 
  Add-Content $fileName "</tr>" 
} 

$global:a=0 
 
Function increment
{ 
  $global:a++ 
} 


Function Write-CitrixSessionLog {
    <#
    .SYNOPSIS
    Writes a Citrix session log and sends it via email.
 
    .DESCRIPTION
    This function retrieves disconnected sessions from specified Citrix desktop groups, writes a log in HTML format, and sends it via email to the specified recipients.
 
    .PARAMETER FileName
    Specifies the path of the file to which the session log will be written.
 
    .PARAMETER DDC
    Specifies the Delivery Controller (DDC) to connect to.
 
    .PARAMETER SessionSupport
    Specifies the type of session support (SingleSession or MultiSession).
 
    .PARAMETER SessionStateChangeTime
    Specifies the time frame for session state change.
 
    .PARAMETER DesktopGroupName
    Specifies the desktop group name pattern to filter the Citrix desktop groups.
 
    .PARAMETER ExcludedADGroups
    Specifies an array of Active Directory group names to exclude from the session log.
 
    .PARAMETER SmtpServer
    Specifies the SMTP server to use for sending emails.
 
    .PARAMETER FromEmail
    Specifies the sender's email address.
 
    .PARAMETER ToEmail
    Specifies one or more recipient email addresses.
 
    .EXAMPLE
    Write-CitrixSessionLog -FileName "C:\Logs\CitrixSessionLog.html" -DDC "vdurctxddc001" -SessionStateChangeTime "-4:15" -DesktopGroupName "win10-bti-*" -ExcludedADGroups @("Group1", "Group2") -SmtpServer "smtp.example.com" -FromEmail "admin@example.com" -ToEmail "user1@example.com", "user2@example.com"
    Retrieves disconnected sessions from specified Citrix desktop groups, writes a log, and sends it via email to user1@example.com and user2@example.com.
 
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$FileName,

        [Parameter(Mandatory = $true)]
        [string]$DDC,

        [ValidateSet("SingleSession", "MultiSession")]
        [Parameter(Mandatory = $true)]
        [string]$SessionSupport,

        [Parameter(Mandatory = $true)]
        [string]$SessionStateChangeTime,

        [Parameter(Mandatory = $true)]
        [string]$DesktopGroupName ,

        [Parameter(Mandatory = $true)]
        [string[]]$ExcludedADGroups,

        [Parameter(Mandatory=$true)]
        [string] $SmtpServer,

        [Parameter(Mandatory=$true)]
        [string] $FromEmail,

        [Parameter(Mandatory=$true)]
        [string] $ToEmail
    )

    $BTIs = (Get-BrokerDesktopGroup -AdminAddress $DDC -SessionSupport $SessionSupport -filter {name -like $DesktopGroupName}).name

    foreach ($BTI in $BTIs) {
        $names = Get-FilteredDisconnectedSessions -AdminAddress $DDC -SessionStateChangeTime $SessionStateChangeTime -DesktopGroupName $BTI -ExcludedADGroups $ExcludedADGroups

        if ($null -ne $names) {
            writeHtmlHeader $FileName 
            Add-Content $FileName "<table width='100%'><tbody>" 
            writeTableHeader $FileName 
            foreach ($name in $names) {
                writeDiskInfo $FileName $BTI $name -Join "`n"
            }
            Add-Content $FileName "</table>"  
            Add-Content $FileName "<br />Regards,<br />Citrix VDI Team.<br /><br />"
            Add-Content $FileName "<i>Please Note : This is a Automatic generated email,please do not reply back to it.</i><br />"
            writeHtmlFooter $FileName 
        }
    
        Write-Output "-------------------- $BTI --------------------"

        AutomatedSessionLogoffByDG -AdminAddress $DDC -SessionStateChangeTime $SessionStateChangeTime -DesktopGroupName $DesktopGroupName -ExcludedADGroups $ExcludedADGroups
        if(Test-Path $FileName) {
        Send-SessionLogoffEmail -SmtpServer $SmtpServer -FromEmail $FromEmail -ToEmail $ToEmail -FileName $FileName
        Remove-Item -Path $FileName -Force}
    }
}