用于并行监视文件列表的Powershell脚本

用于并行监视文件列表的Powershell脚本,powershell,foreach,scheduled-tasks,Powershell,Foreach,Scheduled Tasks,我们有大约六台服务器,每五分钟通过ftp发送一对文本文件,并更新状态。我们正在尝试将Windows任务调度器和Powershell脚本组合用作监视和警报引擎。脚本正在查找在过去60分钟内未刷新的文件,并在“确定”以外的任何条件下读取这些文件。我们希望计划的任务每五分钟运行一次 当$statusfiles中定义的文件的while条件之一为true时(请参见下面的脚本),脚本将挂起循环同一文件,同时等待条件为false,而不是处理列表中的下一个文件并在后台查看问题文件。我们希望在脚本运行后立即知道任

我们有大约六台服务器,每五分钟通过ftp发送一对文本文件,并更新状态。我们正在尝试将Windows任务调度器和Powershell脚本组合用作监视和警报引擎。脚本正在查找在过去60分钟内未刷新的文件,并在“确定”以外的任何条件下读取这些文件。我们希望计划的任务每五分钟运行一次

当$statusfiles中定义的文件的while条件之一为true时(请参见下面的脚本),脚本将挂起循环同一文件,同时等待条件为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
    }

}

您是否尝试过foreach与工作流并行?您是否尝试过foreach与工作流并行?