Public/Send-SessionLogoffEmail.ps1

<#
.SYNOPSIS
    Sends an email with the Citrix Disconnected Session Status Report.
 
.DESCRIPTION
    The Send-SessionLogoffEmail function sends an email containing the Citrix Disconnected Session Status Report.
 
.PARAMETER SmtpServer
        Specifies the SMTP server used to send the email.
 
.PARAMETER FromEmail
        Specifies the sender's email address.
 
.PARAMETER ToEmail
        Specifies the recipient's email address.
 
.PARAMETER Subject
        Specifies the subject of the email. If not provided, the default subject includes the current date and time.
 
.PARAMETER Priority
        (Optional) Specifies the priority of the email. Default is "High".
 
.PARAMETER AttachFile
        Indicates whether to attach a file to the email.
 
.PARAMETER FileName
        Specifies the path to the file to be attached to the email.
 
.EXAMPLE
    Example 1:
    Send-SessionLogoffEmail -SmtpServer "smtp.example.com" -FromEmail "admin@example.com" -ToEmail "user@example.com" -Subject "Citrix Disconnected Session Report" -AttachFile -FileName "C:\Reports\SessionReport.html"
 
    This example sends an email with the specified SMTP server, sender, and recipient email addresses. It attaches the SessionReport.html file and sets the email subject.
 
 
    Example 2:
    Send-SessionLogoffEmail -SmtpServer "smtp.example.com" -FromEmail "admin@example.com" -ToEmail "user@example.com" -FileName "C:\Reports\SessionReport.html"
 
    This example sends an email without specifying the subject and without attaching the file to email. The default subject includes the current date and time.
 
 
    Example 3:
    Send-SessionLogoffEmail -SmtpServer "smtp.example.com" -FromEmail "admin@example.com" -ToEmail "user@example.com" -Priority "Normal" -AttachFile -FileName "C:\Reports\SessionReport.html"
 
    This example sends an email with the priority set to "Normal" and attaches the SessionReport.html file.
 
 
.NOTES
    Requires appropriate permissions to send emails.
 
#>

Function Send-SessionLogoffEmail {
    param (
        [Parameter(Mandatory=$true)]
        [string] $SmtpServer,
        [Parameter(Mandatory=$true)]
        [string] $FromEmail,
        [Parameter(Mandatory=$true)]
        [string] $ToEmail,
        [string] $Subject,
        [string] $Priority = "High",
        [switch] $AttachFile,
        [string] $FileName
    )

    if ($AttachFile -and -not $FileName) {
        Write-Error "File name is required when using the AttachFile switch."
        return
    }

    $CDate = (Get-Date -Format "dddd, dd. MMMM yyyy hh:mm:ss tt")
    $Subject = "Citrix Disconnected Session Status Report - $CDate EST"
    $smtp = New-Object Net.Mail.SmtpClient($SmtpServer) 
    $email = New-Object System.Net.Mail.MailMessage
    $email.From = $FromEmail
    $email.To.Add($ToEmail)
    $email.Subject = $Subject
    $email.IsBodyHtml = $true

    # Read the content from the HTML file
    if ($FileName -and (Test-Path $FileName -PathType Leaf)) {
        $email.Body = Get-Content $FileName -Raw
    } else {
        Write-Warning "HTML file not found at $FileName. Body will be empty."
    }

    if ($AttachFile) {
        if (-not $FileName) {
            Write-Error "File name is required when using the AttachFile switch."
            return
        }
        $attachment = New-Object Net.Mail.Attachment($FileName)
        $email.Attachments.Add($attachment)
    }

    $email.Priority = [System.Net.Mail.MailPriority]::$Priority
    $smtp.Send($email)

    if ($attachment) {
        $attachment.Dispose()
    }
}