编写Powershell脚本以监视日志文件

时间:2020-09-21 19:26:04

标签: powershell powershell-2.0

我正在尝试编写一个脚本,该脚本将监视日志文件的修改时间。如果文件在最后一分钟受到监视,请给我发送电子邮件,说一切都很好。如果日志文件较旧,则为一分钟,时间在上午6点至晚上9点之间,请给我发送电子邮件。如果在晚上9点至早上6点之间重新启动服务器。除了无论如何都会重新启动服务器

    # Set perameters here
    $uncPath   = 'Unc path to log file' 
    $Server = 'ServerName'
    $min = Get-Date 06:00
    $max = Get-Date 21:00
    $now = Get-Date -Format hh:mm:ss
    
    
    # Look at the Printstream Log Files and check last modified time
    
    $files = (Get-ChildItem -Path $uncPath -Filter '*PrintstreamBroker*' -File | 
              Where-Object { $_.LastWriteTime -Gt (Get-Date).AddMinutes(-1) }).FullName
    
    #If there file is good do nothing, and send email 
    
    if ($files) {
        $Subject = 'Printstream Broker Is Running'
        $message = 'The Printstream Broker Is Running There Is Nothing To Do'
        $emailTo = '1@1.com'
    }
    
    
    
    Else ($min.TimeOfDay -lt $now -and $max.TimeOfDay -gt $now){ 
                    { 
                     $Subject = 'Printstream Broker Is Not Running'
                     $message = 'Printstream Broker Is Not Running Please Log Into $servername and start the Printstream Monarch Broker'
                     $emailTo = '1@1.com'
                    }
            }

            Else {
 
            Restart-Computer –ComputerName $server -Force

            $Subject = 'Monarch Server Rebooted'
            $message = 'The Monarch Server Has Been Rebooted'
            $emailTo = '1@1.com'
        }
      }  
   

# create a email 
$mailParams = @{
    From       = 'PrintstreamBrokerDEV@visionps.com'
    To         = $emailTo
    Subject    = $Subject
    Body       = $message
    SmtpServer = 'smtpserver'
    # any other parameters you might want to use
}
# send the email
Send-MailMessage @mailParams

1 个答案:

答案 0 :(得分:1)

如果还有其他的话。

应该是其他情况。