[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell email problem

Posted on 2014-03-11
11
Medium Priority
?
282 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
10 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
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

Problems using Powershell and Active Directory?

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

Question has a verified solution.

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

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 …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Loops Section Overview

834 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