Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

conditional email in powershell script

Posted on 2013-10-30
2
Medium Priority
?
368 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
2 Comments
 
LVL 40

Accepted Solution

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

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.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Loops Section Overview
Screencast - Getting to Know the Pipeline

926 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