【发布时间】:2021-06-16 18:37:37
【问题描述】:
如果在脚本立即运行时发现模式“空闲” - 它会成功发送电子邮件。问题是它应该能够以开始-睡眠间隔继续查看 while($true) 循环。这不会发生 - 它会等待 60 分钟然后退出 - 即使模式“空闲”被写为最后一行。
我需要在 Start-Job 中使用 while 循环吗?我尝试了使用等待但没有运气的代码:Get-Content $file -Tail 1 -Wait |选择字符串-模式“空闲”-安静
$job = Start-Job {
# Note: $file should be the absolute path of your file
Get-Content $File -Raw | Select-string -Pattern "Idle" -Quiet
}
while($true)
{
# if the job has completed
if($job.State -eq 'Completed')
{
$result = $job|Receive-Job
# if result is True
if($result)
{
$elapsedTime.Stop()
$duration = $elapsedTime.Elapsed.ToString("hh\:mm\:ss")
# .... send email logic here
# for success result
break #=> This is important, don't remove it
}
# we don't need a else here,
# if we are here is because $result is false
$elapsedTime.Stop()
$duration = $elapsedTime.Elapsed.ToString("hh\:mm\:ss")
# .... send email logic here
# for unsuccessful result
break #=> This is important, don't remove it
}
# if this is running for more than
# 60 minutes break the loop
if($elapsedTime.Elapsed.Minutes -ge 60)
{
$elapsedTime.Stop()
$duration = $elapsedTime.Elapsed.ToString("hh\:mm\:ss")
# .... send email logic here
# for script running longer
# than 60 minutes
break #=> This is important, don't remove it
}
Start-Sleep -Milliseconds 500
}
Get-Job|Remove-Job
【问题讨论】:
-
这是因为get-content 只完成一次而不在循环内吗?
标签: powershell