Solved

Sending email using powershell Send-MailMessage in a batch file.

Posted on 2014-10-16
12
1,432 Views
Last Modified: 2014-11-04
If power shell is first started; email can be successfully sent using the command line.

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 and then stops

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'

I know both versions 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 PowerShell, 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
0
Comment
Question by:epmmis
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 39

Expert Comment

by:footech
ID: 40385629
I don't have an answer for you, but just tested from a cmd.exe window and it worked just fine.
You might also try
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'}"

Open in new window

0
 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40385758
Hello,

I would imagine that this is because when these commands are ran they are not within a PowerShell environment. Using parameters will work, -Command being a parameter this will accept the code block and pass it to a powershell environment.

Another way is to call your PowerShell Scripts from within a batch file. (PowerShell.exe C:\Scripts\MyScript.ps1 - As an example).

I hope this helps.

Regards

Michael
0
 

Author Comment

by:epmmis
ID: 40386033
Michael
Changed the batch file to call the PS script.

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'

The email did not sent.

SendEmail.ps1 was manually ran by right mouse clicking on the file and then selected "Run With PowerShell"
Still no email.

Again, Email was successfully sent by copying/pasting the script from SendEmail.ps1 into a PS window.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:epmmis
ID: 40386035
FooTech

Changed the batch file to this format.

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'}"

No Email using this format.
0
 
LVL 39

Expert Comment

by:footech
ID: 40386081
Try on another machine.
0
 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40387466
Hello,

If you run the script manually and it doesn't work there is a problem with your code, there is no reason that I can see if the script is correct why it cannot be called from a batch file. I have needed to do this many times before, which have always succeeded.

I suggest you review the code in your script and try again, run the script manually until the email sends, then call it from a batch file using my suggested method.

If you are unsure if the contents of your script you are calling is correct, please post this so I or another expert can review this for you :)

In addition, you will also need to ensure that the machine you are relaying email from is allowed to do so to the send connector on the target email server.

All the best.

Michael
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40390995
I cannot see any issue here either. Are you able to trace what is received by the SMTP server?
Is what you showed the exact command line?
Since you verified that To, From and SmtpServer are all evaluated correctly, the reason has to be in the Subject or Body line.
0
 

Accepted Solution

by:
epmmis earned 0 total points
ID: 40400630
I found the solution.

In the batch file I call the PS1 script this way.
  powershell.exe   -command " & {C:\scripts\SendEmail.ps1}"

The SendEmail.ps1 file
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"

The code above by itself does not send email
By appending this code to the SendEmail.ps1 file, the email is sent.

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
      Write-Host "Press any key to continue..."
        $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
0
 

Author Comment

by:epmmis
ID: 40406100
I've requested that this question be closed as follows:

Accepted answer: 0 points for epmmis's comment #a40400630
Assisted answer: 200 points for footech's comment #a40386081
Assisted answer: 150 points for MichaelIanClaridge's comment #a40387466
Assisted answer: 150 points for Qlemo's comment #a40390995

for the following reason:

It was the only thing which made the email go.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40400664
That does not make sense for me. It suggests there is a timing issue.

If you have a PS script to call, you do that with
powershell.exe   -file C:\scripts\SendEmail.ps1

Open in new window

I guess the closing double quote in the SendEmail.ps1 file (without the wait for key stuff) is a typo, because it should be single quote ...

Further, I  will have to object. Fine that you want to provide points, but there is nothing in our comments helping with your "solution".
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40406101
Objection: See my last comment. The question should either be deleted, or http:#a40400630 accepted.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
getting combo box selected item into variable 9 42
text file manipulation 2 17
Powershell command 2 24
Upgrade Exchange 2013 to Exchange 2016 8 42
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question