If powershell is first started; email can be successfully sent using the PS command line.
This is the command line used to send email. It successfully sends email.
PS C:\> Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App' -Body 'This is the body of the message'
From a batch file or the command line this command fails to send email. There are no errors. The command runs without error or warning.
Different formats were used in the batch file.
powershell Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App' -Body 'This is the body of the message'
powershell -command "& {Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App' -Body 'This is the body of the message'}"
Then changed the batch file to call PS1 script "SendEmail.ps1".
Batch File
powershell.exe C:\scripts\SendEmail.ps1
SendEmail.ps1
Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App' -Body 'This is the body of the message'
When the batch file failed, manually ran SendEmail.ps1 from Windows by right mouse clicking on the PS1 file and selecting "Run with PowerShell"
No email was sent.
I know all versions of the scripts/command line/batch file connect to the mail server. If the "-To" or "-From" address is intentionally misspelled the mail server correctly reports the error.
Why does email succeed when it is sent from within the PowerShell window, but fails when sent from the command line or batch file?
Platform: Windows 2008 R2 server
Powershell version: 2
Mail Server: MDaemon made by Alt-N