Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell email problem

Posted on 2014-03-11
11
Medium Priority
?
279 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

636 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