Share via


Send-MailMessage how to pass -attachment variable?

Question

Monday, September 28, 2015 10:12 PM

I'm trying to run the following script. It works fine until it gets to the -attachment option. It doesn't like the log file variable.

$Log = "c:\logs\date:~-4,4%-%date:~-10,2%-%date:~-7,2%.log"

PS C:\Users\administrator.ADVOCACYINC> Send-MailMessage -From "[email protected]" -to "[email protected]" -Subject "tes
t log" -Body "test body" -Smtpserver "mailserver.com" -port "25" -attachment $Log

All replies (3)

Monday, September 28, 2015 10:39 PM ✅Answered

You cannot use CMD string parsing in PowerShell:

$Log = "c:\logs$([datetime]::Today.ToString('yyyyMMdd')).log"

\(ツ)_/


Monday, September 28, 2015 10:22 PM

This works fine for me. What's the actually error message, or do you not get one?


Tuesday, September 29, 2015 1:27 AM

Here is what I ended up doing.

$date = get-date -format yyyy-MM-dd
$log = "c:\logs\$date" + ".log" 
Send-MailMessage -From "[email protected]" -to "[email protected]" -Subject "Backup Log for helpdesk on $date" -Body (Get-Content $log) -Smtpserver "mailserver.com" -port "25" -attachment $log