Powershell包括计划任务中的额外列

时间:2019-03-01 16:17:22

标签: powershell

我有下面的Powershell代码片段,它们在Windows Task Scheduler中查找特定的任务,循环遍历每个任务并将其放入电子邮件中-效果很好。目前,它给出了两列-“任务”和“状态”。

但是,如何在每个任务的结果中也引入“ LastRunTime”值?我添加了“ LastRun”标头:

<h4>Task Status:</h4>

<table>
    <tr>
        <td class="colort">Task</td>
        <td class="colort">Status</td>
        <td class="LastRun">Status</td>
    </tr>
"@

$EmailDown = @"
</table>
</body>
"@


foreach ($Task in $Tasks){
    switch -Regex ($Task){
        {$DailyTasks -contains $Task}{
            $TaskRunTime = (Get-ScheduledTask $Task | Get-ScheduledTaskInfo).LastRunTime
            $Difference = (New-TimeSpan -Start $TaskRunTime).TotalHours
            $IntervalCheck = 12

            switch -Regex ($Difference){
                {($Difference -gt "$IntervalCheck")}{
                    $Status = "BAD"

                    $EmailTemp = @"
    <tr>
        <td class="colorm">$Task</td>
        <td class="colorr">$Status</td>
        <td class="colorr">$LastRun</td>
    </tr>
"@
                }
                {$Difference -lt "$IntervalCheck"}{
                    $Status = "OK"

                    $EmailTemp = @"
    <tr>
        <td class="colorm">$Task</td>
        <td>$Status</td>
    </tr>
"@
                }
            }

            $EmailResult = $EmailResult + $EmailTemp
            $EmailTemp = ""
        }
    }
}

0 个答案:

没有答案