首页 > 解决方案 > 如果发现错误代码,Powershell 检查日志文件并重新启动服务

问题描述

我对 PowerShell 很陌生,我正在尝试运行一个脚本来检查日志文件最后 5 行中的错误代码。如果脚本发现错误,它应该重新启动服务,否则它应该循环回到顶部并再次执行。

if(Get-Content C:\x\x\x\Log.log -last 5  | Select-String -pattern "Error" -Quiet){
  Restart-Service -Name xxx -Force
}
else

标签: powershellloopslogging

解决方案


到目前为止,您的代码是正确的。但是,该-last参数在 中不存在Get-Content。试试看-Tail

更多信息:https ://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7.1


推荐阅读