powershell - 如果发现错误代码,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
解决方案
到目前为止,您的代码是正确的。但是,该-last
参数在 中不存在Get-Content
。试试看-Tail
。
推荐阅读
- python - 如果我使用 swig,我是否必须构建多个链接的 .so/.dll 与我想支持的每个 python 版本?
- python-3.x - 为什么我的矩阵完成程序在训练时,训练集上的 RMSE 会下降,而测试集上的 RMSE 会上升?
- tensorflow-lite - 模型转换时如何更改批量大小?
- python - 通过pywin32读取进程内存给出不正确的结果
- css - 为什么文本对齐会影响子 div,但不会影响字体大小?
- r - 如何将点添加到 freqpoly 图?
- python - 将文本转换为列表的程序
- javascript - 为什么这些 JavaScript 方程会输出不同(但几乎相同)的答案?
- c++ - win10如何调试崩溃?
- javascript - 数组中有值,但无法访问它们,可能是什么问题?