powershell get-content需要IF

时间:2016-06-04 17:33:05

标签: powershell

    while($true)
{
  Get-Content C:\Steam\steamapps\startup\server_console_*.log -wait | where {$_ -match "Game finished."} |
  foreach { 
     Start-Sleep -s 30
     restart-computer -Force

  }
}

嗨! "游戏结束时,我希望系统重启。"列出两次日志,我该怎么办?感谢

1 个答案:

答案 0 :(得分:0)

while ($true)
{
  if ((Get-Content C:\Steam\steamapps\startup\server_console_*.log | where {$_ -match "Game finished"}).Count -gt 1)
  {
    restart-Computer -force
  }
  start-sleep -seconds 30
}

在我看来,最好不要在PS中使用while $ true循环,但要有一个简单的if (..) {restart-Computer}和日程安排(计划任务),脚本每分钟运行5分钟等等。这将是资源匮乏少得多:

if ((Get-Content C:\Steam\steamapps\startup\server_console_*.log | where {$_ -match "Game finished"}).Count -gt 1)
  {
    restart-Computer -force
  }