Skip to main content

Messaging

This page groups the messaging functions used to send emails and conditional reports. For full and always-up-to-date details, use Get-Help <FunctionName> -Detailed (or -Examples).

Send-Mail

Send emails via SMTP with support for CC/BCC, attachments, SSL/TLS, and credentials.

Syntax

Send-Mail -SMTPServer <String> -From <String> -To <String[]> -Subject <String> -Body <String>
[-SMTPPort <Int>] [-Cc <String[]>] [-Bcc <String[]>] [-AttachmentPath <String[]>]
[-PlainText] [-Credential <PSCredential>] [-UseSsl]

Parameters

ParameterTypeDescriptionRequiredDefault
SMTPServerStringSMTP host name or IP address.Yes-
FromStringSender e-mail address.Yes-
ToString[]One or more recipient e-mail addresses.Yes-
SubjectStringE-mail subject.Yes-
BodyStringMessage body (HTML by default).Yes-
SMTPPortIntSMTP port (for example 25, 465, 587).No25
CcString[]CC recipient addresses.NoNone
BccString[]BCC recipient addresses.NoNone
AttachmentPathString[]One or more file paths to attach.NoNone
PlainTextSwitchSends body as plain text instead of HTML.NoFalse
CredentialPSCredentialCredentials used for authenticated SMTP.NoNone
UseSslSwitchEnables SSL/TLS for SMTP connection.NoFalse

Example

Send-Mail `
-SMTPServer "smtp.contoso.com" `
-UseSsl `
-Credential (Get-Credential) `
-From "user@contoso.com" `
-To "ops@contoso.com" `
-Subject "Nightly job completed" `
-Body "<p>Done</p>"

Send-ReportIfChanged

Finalize and send an HTML report only when changes are detected.

Syntax

Send-ReportIfChanged -ModCounter <Int> -MailBody <String> -SmtpServer <String> -From <String> -To <String[]>
-Subject <String> [-SendLogs <Boolean>] [-AttachmentPath <String[]>]
[-ForceMailTo <Boolean>] [-LogLocation <String>]

Parameters

ParameterTypeDescriptionRequiredDefault
ModCounterIntNumber of detected changes used to decide whether to send.Yes-
MailBodyStringHTML body content for the report e-mail.Yes-
SmtpServerStringSMTP host used to send the report.Yes-
FromStringSender e-mail address.Yes-
ToString[]One or more recipient e-mail addresses.Yes-
SubjectStringSubject line for the report e-mail.Yes-
SendLogsBooleanInclude log artifacts when available.NoTrue
AttachmentPathString[]Additional attachment file paths.NoNone
ForceMailToBooleanForces send behavior even when no changes are detected.NoFalse
LogLocationStringLog file/location used for diagnostics.NoNone

Example

$result = Send-ReportIfChanged `
-SendLogs $true `
-ModCounter 4 `
-MailBody $mailBody `
-SmtpServer "smtp.contoso.com" `
-From "user@contoso.com" `
-To "ops@contoso.com" `
-Subject "User sync report"