?
Solved

Powershell email problem

Posted on 2014-03-11
11
Medium Priority
?
277 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 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