我们有大约六打服务器ftp一对文本文件,状态每五分钟更新一次。我们尝试使用Windows任务计划程序和Powershell脚本的组合作为监视器和警报引擎。该脚本正在查找在过去60分钟内未刷新的文件,并在“确定”以外的任何条件下读取文件。我们希望计划的任务每五分钟运行一次。
当$ statusfiles中定义的文件的某个while条件为真时(参见下面的脚本),脚本在等待条件为false而不是处理下一个文件时挂起同一个文件。列出并在后台查看问题文件。如果任何状态文件报告了问题,我们想在脚本运行后立即知道。
在我的第一个版本中,我使用了If-ElseIf-Else方法。这给了我正在寻找的即时通知,但如果我不循环警报条件,我不知道如何设置重试间隔($ retryinterval)给我们一个宽限期来修复底层问题。
是否可以使foreach方法与$ statusfiles中定义的所有文件并行运行?这会是系统密集型的吗?还是有其他方法我没有看到?
提前感谢您的帮助!
$erroractionpreference="SilentlyContinue"
$filepath = "\\Server\DirectoryA\DirectoryB\DirectoryC"
$statusfiles = "$filepath\opmessage21.txt","$filepath\pgmessage21.txt","$filepath\opmessage23.txt","$filepath\pgmessage23.txt","$filepath\opmessage24.txt","$filepath\pgmessage24.txt","$filepath\opmessage25.txt","$filepath\pgmessage25.txt","$filepath\opmessage26.txt","$filepath\pgmessage26.txt"
$agelimit = "60"
$retryinterval = "1800"
$to = "recipient1@email.com","recipient2@email.com","recipient3@email.com","recipient4@email.com"
$from = "alert@email.com"
$smtp = "smtp.server.com"
$port = "25"
function send-email
{
Send-MailMessage -Body "$body" -to $to -from $from -Subject "$subject" -smtp $smtp
}
foreach ($statusfile in $statusfiles) {
# Initialize the filestale and file error variables and strip the path and extension from $statusfile
$filestale = 0
$fileerror = 0
$messageid = $statusfile.split('\.')[-2]
# Get LastWriteTime of the file and alert the admin if LastWriteTime is older than X minutes
$lastupdated = (get-childitem $statusfile).LastWriteTime
while ($lastupdated -lt (get-date).AddMinutes(-$agelimit)) {
$filestale = 1
write-host "$messageid is older than $agelimit minutes. (Last updated: $lastupdated)"
$subject = "$messageid is older than $agelimit minutes"
$body = "Last updated: $lastupdated"
send-email
start-sleep -s $retryinterval
$lastupdated = (get-childitem $statusfile).LastWriteTime
}
# Alert the admin the file is no longer outdated
if ($filestale = 1) {
$filestale = 0
write-host "$messageid has been refreshed. (Last updated: $lastupdated)"
$subject = "$messageid has been refreshed"
$body = "Last updated: $lastupdated"
send-email
}
# Check the file for an error and alert the admin if the status is not OK
$messagecontents = Get-Content -Path "$statusfile" -Raw
while ($messagecontents -ne 'OK') {
$fileerror = 1
write-host "$messageid reported the following error: $messagecontents (Last updated: $lastupdated)"
$subject = "Error reported by $messageid"
$body = "$messagecontents (Last updated: $lastupdated)"
send-email
start-sleep -s $retryinterval
$messagecontents = Get-Content -Path "$statusfile" -Raw
$lastupdated = (get-childitem $statusfile).LastWriteTime
}
# Alert the admin the status is now OK
if ($fileerror = 1) {
$fileerror = 0
write-host "$messageid indicates status OK. (Last updated: $lastupdated)"
$subject = "$messageid indicates status OK"
$body = "Last updated: $lastupdated"
send-email
}
}