Home > Back-end >  How to add a date in mail subject using powershell
How to add a date in mail subject using powershell

Time:11-30

I'm writing a script that search the past 7 days Audit Logs report from Microsoft Security Compliance center. The script will run every Sunday night and will send a report to my manager for that week.

In my script, I have very basic subject but I'm thinking about adding a Date range in the subject so that It'll be more helpful but I'm not sure how to do that so I'll be really appreciated If I can get help or suggestion.

I want to have something in the subject like "Audit Log report from 11/22/2021-11/28/2021"


$logFile = "C:\AuditLogSearch\AuditLogSearchLog.txt"
$outputFile = "C:\AuditLogSearch\AuditLogRecords.csv"
[DateTime]$start = [DateTime]::UtcNow.AddDays(-7)
[DateTime]$end = [DateTime]::UtcNow
$record = "Discovery"
$resultSize = 5000
$intervalMinutes = 60


#####################
#  Send Automatic Email
#####################


$OL = New-Object -ComObject outlook.application
Start-Sleep 5
$mItem = $OL.CreateItem("olMailItem")

$mItem.To = "[email protected]"
$mItem.Subject = "Weekly Audit Log Report"
$mItem.Attachments.Add("C:\AuditLogSearch\AuditLogSearchLog.txt")
$mItem.Body = "SENT FROM POWERSHELL"

$mItem.Send()

CodePudding user response:

You already have the necessary parts:

[DateTime]$start = [DateTime]::UtcNow.AddDays(-7)
[DateTime]$end = [DateTime]::UtcNow

Just need to change $mItem.Subject = "Weekly Audit Log Report" for:

$mItem.Subject = "Audit Log report from {0}-{1}" -f
$start.ToShortDateString(), $end.ToShortDateString()

CodePudding user response:

By using .ToShortDateString() you can get the output your want.

...
$mItem.Subject = "Weekly Audit Log Report $($start.ToShortDateString()) - $($end.ToShortDateString())"
...
  • Related