Solved

Powershell email problem

Posted on 2014-03-11
11
271 Views
Last Modified: 2014-03-11
I have a script to email details from files. The first half works fine but the second, else, statement errors saying it does not like the ` . They are in the first part of the script and work fine. I have used them before. Here is the email script. Any suggestions?
IF ((Get-Content C:\MonitorFolder\ServersDown.txt) -ne $null ){
		Send-MailMessage -From 'ServerStatusCheck@company.com' -To 'Support@company.com' `
  -SmtpServer 'smtp4.company.com' `
  -Subject 'THIS IS ONLY A TEST ALERT - ALERT - ALERT Server(s) Down Please Ignore (PS Script)' `
  -BodyAsHtml `
    -Body  "
			<b><font color=red>Servers are Down<b><font color=red></b></font>
				   	$((Get-Content C:\MonitorFolder\ServersDown.txt) -join "<br>"))
		"
		}
	else
	{
		Send-MailMessage -From 'ServerStatusCheck@company.com' -To 'Support@company.com' `
  -SmtpServer 'smtp4.company.com' `
  -Subject 'THIS IS ONLY A TEST of the Production Server Status Check Please Ignore (PS Script)' `
  -BodyAsHtml `
    -Body  "
	<b><font color=Green>The Following Were tested and are up and can be logged into </b><br><br>
     $((Get-Content C:\MonitorFolder\ServersUp.txt) -join "<br>")
	   ("`n")
	   	"
		 -BodyAsHtml }

Open in new window

0
Comment
Question by:jimmylew52
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39920995
On the else branch you have 2 -BodyAsHtml and this line
("`n")
which I don't know what you wanted with it. Since it's HTML, all new line characters are ignored anyway and you need <br> tags.

HTH,
Dan
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39921012
The ("`n") is the line that gives you the error. The first " closes the -Body expression, so you're left with `n"), which chokes the interpreter.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39921181
Here is where I am now:

else{
            Send-MailMessage -From 'ServerStatusCheck@systrends.com' -To 'support@systrends.com'`
            -SmtpServer 'smtp4.systrends.com'`
            -Subject 'THIS IS ONLY A TEST of the Production Server Status Check Please Ignore (PS Script)'
             -BodyAsHtml`
             -Body  "
          ("<b><font color=Green>The Following Were tested and are up and can be logged into </b><br><br>")
            $((Get-Content C:\MonitorFolder\ServersUp.txt) -join "<br>")
             }

Still errors complaining about -body and -bodyashtml
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39921227
I suggest you do it this way:
else {
    $subject="THIS IS ONLY A TEST of the Production Server Status Check Please Ignore (PS Script)"
    $body="<b><font color=Green>The Following Were tested and are up and can be logged into </b><br><br>
         $((Get-Content C:\MonitorFolder\ServersUp.txt) -join '<br>')"

    Send-MailMessage -From 'ServerStatusCheck@company.com' -To 'Support@company.com' `
      -SmtpServer 'smtp4.company.com' `
      -Subject $subject -Body $body -BodyAsHtml 
  }

Open in new window


Easier to see the syntax.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39921271
Tried it and it prints the contents of Serversup.txt to the screen instead of emailing the list.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39921286
Minor modification and it prints to screen and emails.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39921307
It should email. That was the purpose, right?

I don't see anything on your code that prints to the screen.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39921342
I forgot about an entry further back in the script the prints to screen. I have comented that out now.

Yes it should email. All I changed was the position of the lines and it started working as it should.
0
 
LVL 1

Author Closing Comment

by:jimmylew52
ID: 39921348
Works Great, Thank You!!!!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39921362
Glad I could help!
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

A brief introduction to what I consider to be the best editor for PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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