编写一个 powershell 脚本来监控日志文件

Writing a powershell script to monitor a log file

我正在尝试编写一个脚本来监视日志文件的修改时间。如果文件在最后一刻受到监控,请给我发一封电子邮件,说一切都很好。如果日志文件早一分钟并且时间在早上 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

你有 if else else。

应该是if elseif else