在 PowerShell 脚本的 While($true) 循环中使用作业

2024-01-15

如果在立即运行脚本时发现模式“空闲” - 它会成功发送电子邮件。问题是它应该能够在 while($true) 循环内以开始睡眠间隔继续查找。这不会发生 - 它会等待 60 分钟然后退出 - 即使模式“Idle”被写入最后一行。

我需要在开始作业中使用 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 https://learn.microsoft.com/powershell/module/microsoft.powershell.management/get-content's -Wait切换以(近)实时地继续检查文件中的新内容(每秒检查一次新内容)。

    • 然而,这样做需要等待无限期地,并且仅当目标文件被删除、移动或重命名时才结束。
  • 因此,与-Wait已申请,您的工作可能永远不会达到状态'Completed'- 但鉴于此,无需等待Receive-Job https://learn.microsoft.com/powershell/module/microsoft.powershell.core/receive-job可以在作业运行时接收作业输出(当作业可用时)。

  • 但是,您不能使用Select-String https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-string's -Quietswitch,因为它只会输出one结果,即$true一旦first找到匹配项 - 即使稍后添加内容也不会产生进一步的输出also火柴。

因此,您可能需要如下所示的内容:

$job = Start-Job {
  # Use Get-Content with -Wait, but don't use Select-String with -Quiet
  Get-Content $File -Raw -Wait | Select-string -Pattern "Idle"
}

while ($true)
{
  # Check for available job output, if any.
  if ($result = $job | Receive-Job) {

      $duration = $elapsedTime.Elapsed.ToString("hh\:mm\:ss")
          
      # .... send email logic here
      # for success result
      break

  }

  # 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
      
      # Forcefully remove the background job.
      $job | Remove-Job -Force

      break
  }
  
  Start-Sleep -Milliseconds 500
}

Note:

  • $job | Receive-Job如果碰巧没有可用的输出,则不产生任何输出,或者一个或多个[Microsoft.PowerShell.Commands.MatchInfo] https://learn.microsoft.com/en-US/dotnet/api/Microsoft.PowerShell.Commands.MatchInfo报告的实例Select-Object.

  • 使用此命令作为if-陈述有条件的(结合分配给$result) 表示一个或多个[Microsoft.PowerShell.Commands.MatchInfo]实例使条件评估为$true,基于 PowerShell 的隐式 to-Boolean 强制逻辑 - 请参阅底部部分这个答案 https://stackoverflow.com/a/53108138/45375.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 PowerShell 脚本的 While($true) 循环中使用作业 的相关文章

随机推荐