使用电子邮件通知记录多台计算机的日志

时间:2016-09-26 05:56:18

标签: powershell event-log

我在下面有这个PowerShell代码,它从计算机列表中收集事件日志,然后向收件人发送电子邮件。

我的问题是,如何更改脚本以仅在收集值/日志时发送电子邮件

$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"

1 个答案:

答案 0 :(得分:0)

使用if子句检查$body是否存在:

$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"
}