Solved

Powershell email problem

Posted on 2014-03-11
11
266 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
  • 5
  • 5
11 Comments
 
LVL 34

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 34

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
 
LVL 34

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

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

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 34

Expert Comment

by:Dan Craciun
ID: 39921362
Glad I could help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now