I'm trying to invoke an inline powershell script from Task Scheduler to send an email when a particular event is triggered. I can't use the native 'Send an e-mail' action in the Task Scheduler action window because the SMTP server requires SSL and there's no way to specify this in the action window. So I'm looking to 'Start a program' and invoke something to send email but I want to avoid using 3rd party applications such as sendEmail if possible so was hoping to be able to invoke an inline powershell script similar to the following.
Setting the 'Program/script' field to powershell and the arguments field to:
-Command "{Send-MailMessage -From "Name <[email protected]>" -To "[email protected]" -Subject "Test Subject" -Body "Test body at $(Get-Date -Format "dd/MM/yyyy")." -SmtpServer "smtp.domain.com" -UseSSL}"
This obviously doesn't work due to the nested quotes etc, so I've been trying different variations in command prompt, but I can't for the life of me figure out which characters I need to escape and how to escape them.
Any help would be much appreciated.
PowerShell -Command "& {Get-EventLog -LogName Application}"
PowerShell -Command "& {Send-MailMessage -From 'Name ' -To '[email protected]' -Subject 'Test Subject' -Body ('Test body at {0}.' -f (Get-Date -Format 'dd/MM/yyyy')) -SmtpServer 'smtp.domain.com' -UseSSL}"
This should work:
powershell -Command 'Send-MailMessage -From "Name <[email protected]>" -To "[email protected]" -Subject "Test Subject" -Body "Test body at $(Get-Date -Format "dd/MM/yyyy")." -SmtpServer "smtp.domain.com" -UseSSL'
You can use single quotes around double quotes, and you don't need the curly brackets around the command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With