I have this PowerShell code below that gathers eventlogs from a list of computers and then sends email to the recipients.
My question is, how to change the script to send an email only when there is value / log gathered?
$logReport = "C:\Logs.html"
# CSS style
$css= "<style>"
$css= $css+ "BODY{ text-align: center; background-color:white;}"
$css= $css+ "TABLE{ font-family: 'Lucida Sans Unicode', 'Lucida Grande', Sans-Serif;font-size: 12px;margin: 10px;width: 100%;text-align: center;border-collapse: collapse;border-top: 7px solid #004466;border-bottom: 7px solid #004466;}"
$css= $css+ "TH{font-size: 13px;font-weight: normal;padding: 1px;background: #cceeff;border-right: 1px solid #004466;border-left: 1px solid #004466;color: #004466;}"
$css= $css+ "TD{padding: 1px;background: ##FFFFFF;border-right: 1px solid #004466;border-left: 1px solid #004466;color: #669;hover:black;}"
$css= $css+ "TD:hover{ background-color:#e5f7ff;}"
$css= $css+ "</style>"
$StartDate = (get-date).AddDays(-12)
$body = Get-Content 'servers.txt' | ForEach-Object {
Get-WinEvent -ComputerName $_ -FilterHashtable @{logname="System"; Level=1,2,3; starttime=$StartDate}
}
# Convert to HTML style
$body | ConvertTo-HTML -Head $css MachineName,LogName,LevelDisplayName,ID,TimeCreated,Message > $logreport
# EMAIL Properties
$rcpts = "user@domain.com"
$smtpServer = "smtp.domain.com"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($rcpts)
$msg.From = "user@domain.com"
$msg.Subject = "Subject"
$msg.IsBodyHTML = $true
$msg.Body = get-content $logreport
$smtp.Send($msg)
$body = "Hello"
Just check whether $body
is present using an if clause:
$logReport = "C:\Logs.html"
# CSS style
$css= "<style>"
$css= $css+ "BODY{ text-align: center; background-color:white;}"
$css= $css+ "TABLE{ font-family: 'Lucida Sans Unicode', 'Lucida Grande', Sans-Serif;font-size: 12px;margin: 10px;width: 100%;text-align: center;border-collapse: collapse;border-top: 7px solid #004466;border-bottom: 7px solid #004466;}"
$css= $css+ "TH{font-size: 13px;font-weight: normal;padding: 1px;background: #cceeff;border-right: 1px solid #004466;border-left: 1px solid #004466;color: #004466;}"
$css= $css+ "TD{padding: 1px;background: ##FFFFFF;border-right: 1px solid #004466;border-left: 1px solid #004466;color: #669;hover:black;}"
$css= $css+ "TD:hover{ background-color:#e5f7ff;}"
$css= $css+ "</style>"
$StartDate = (get-date).AddDays(-12)
$body = Get-Content 'servers.txt' | ForEach-Object {
Get-WinEvent -ComputerName $_ -FilterHashtable @{logname="System"; Level=1,2,3; starttime=$StartDate}
}
if ($body)
{
# Convert to HTML style
$body | ConvertTo-HTML -Head $css MachineName,LogName,LevelDisplayName,ID,TimeCreated,Message > $logreport
# EMAIL Properties
$rcpts = "user@domain.com"
$smtpServer = "smtp.domain.com"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($rcpts)
$msg.From = "user@domain.com"
$msg.Subject = "Subject"
$msg.IsBodyHTML = $true
$msg.Body = get-content $logreport
$smtp.Send($msg)
$body = "Hello"
}