Solved

conditional email in powershell script

Posted on 2013-10-30
2
352 Views
Last Modified: 2013-10-30
This script works but errors if the file it is reading is empty.

How can i make the email conditional on the file not being empty.

 $Line = Get-Content E:\MonitorFolder\PingResults.txt
   
  Send-MailMessage -From 'Lost_Network_Connection_16@company.com' -To 'me@company.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Lost Network Connection' `
  -BodyAsHtml `
    -Body  ($Line -join "<br>")
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
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39611844
Try...
 $Line = Get-Content E:\MonitorFolder\PingResults.txt
  
If ($line -ne $null) {
  Send-MailMessage -From 'Lost_Network_Connection_16@company.com' -To 'me@company.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Lost Network Connection' `
  -BodyAsHtml `
    -Body  ($Line -join "<br>") 
}

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39612116
Works, thanks
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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