Solved

conditional email in powershell script

Posted on 2013-10-30
2
350 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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