【问题标题】:Conditional usage of get-eventlogget-eventlog 的条件使用
【发布时间】:2014-06-09 16:50:46
【问题描述】:

我是 Powershell 的 100% 新手,我环顾四周,并没有真正找到关于这个主题的很多内容,但如果其他地方已经回答了这个问题,我深表歉意。

有一个服务不时停止运行,通常是每天早上 7 点到 8 点左右,我可以恢复其功能的唯一方法是重新启动服务。与软件相关。

我每 15 分钟解析一次事件日志以查找特定错误。

$lookForError = get-eventlog -before $currentDate -after $pastDate -logname application -source "Windows Backup"


$itCrashed = $FALSE

$lookForError
<#
while($itCrashed = false)
{
    if ($lookForError -eq )

}#>

这里的大致思路,就是检查事件是否发生,如果发生了,将itCrashed改为true,退出循环,发起服务重启命令。

我不太确定如何使用 powershell 检测到上述错误的存在。

【问题讨论】:

  • 发生这种情况时服务本身会停止吗?
  • 没有服务只是不幸挂起,但它确实在日志中产生错误,它会产生一个“错误应用程序”错误和访问冲突错误。
  • 事件日志中出现的错误是什么?您是否必须搜索文本,还是会引发特定错误#?
  • 错误产生的一些模糊数据有 EventID(1000),应用程序错误是提供者。我曾计划使用“应用程序错误”提供程序和事件 ID 来检测。不确定这是否能回答您的问题。
  • 有什么方法可以从 get-eventlog 的结果中提取这两个东西(事件 ID 和提供者),并在 if 语句中使用它们的值来查看返回的错误是否是错误我在找吗?

标签: powershell


【解决方案1】:

这样的事情应该会让你走上正轨......

$lookForError = get-eventlog -before $currentDate -after $pastDate -logname application -source "Windows Backup" | Where-Object {$_.EventID -eq 1000}

这将通过事件 ID 限制结果。从那里你应该能够假设长度大于零意味着你应该重新启动服务......

if($lookForError.Length -gt 0) {
    # Restart service
    }

为了更好地处理大型事件日志,请使用此选项。谢谢@mjolinor!

# First we configure a file to store the last recorded index
$IndexFile = "C:\Users\Public\serviceindex.txt"
if (Test-Path $IndexFile) {
    [Int32]$lastIndex = Get-Content $IndexFile
    } else {
    $lastIndex =0
    }
$eventPadding = 100

# Next we get the newest index and calculate the difference, using this to limit the results
$newest = Get-EventLog -LogName Application -Newest 1
$records = $newest.Index-$lastIndex+$eventPadding
$lookForError = Get-EventLog -LogName Application -source "Windows Backup" -Newest $records | Where-Object {$_.EventID -eq 1000}

# Finally we restart the service if there was an error and update the index file
if($lookForError.Length -gt 0) {
    # Restart service
    $lookForError.Index | Out-File $IndexFile
    }

【讨论】:

  • 与我写的非常相似,但我会执行-after (get-date).addminutes(-15) 并跳过-before,然后我会每15 分钟从任务调度程序调用该脚本。射击,你可以只做 (pseudocode) if(get-eventlog -source app -after (get-date)-15 -source "winbackup" | where{$_.eventid -eq 1000}){Restart-Service "WinBackup"} 如果你愿意,把它全部写成一行,然后直接从任务调度程序调用该行 (powershell -noprofile & {code goes此处}) 并跳过将其保存为脚本。
  • Get-EventLog -After 在大型事件日志上非常慢(它从日志的开头开始读取)。如果您从最新事件中获取并保存索引号,并将其用作下一次搜索的起点,则可以大大加快搜索速度。
  • @mjolinor -Index 采用 Int32。您将如何使用它作为起点,而不必输出整个结果集?
  • 索引号是连续的。在下一次通过时,重新读取最新的日志条目并从中获取索引。从最新索引中减去最后一个索引号,并为那么多日志条目减去 Get-EventLog -Newest。如果您认为在计算时可能会遗漏条目,您可以将其填充几十个条目,然后按时间戳对结果进行后期过滤。
  • 不错!我会看到有关更新答案的信息。
【解决方案2】:
while ( ((Get-Date).Hour -gt 6) -and ((Get-Date).hour -lt 9) ) {

        $lastTested = (get-date).AddMinutes(-15)

        $potentialLogs = Get-EventLog -LogName Application -After $lastTested | where { ( $_.instanceid -eq 1000 ) -and  ($_.source -eq "Application Error")  }

        $potentialLogs | foreach { 

            if( ($_.ReplacementStrings -split '\n') -match 'w3wp.exe' ) {

                restart-service -name ## [insertServiceNameHere]

            }
        }
        Start-Sleep -Seconds 900  ## 15mins
}

据此answer

Internet 信息服务 (IIS) 工作进程是运行 Web 应用程序的 Windows 进程 (w3wp.exe),负责处理发送到特定应用程序池的 Web 服务器的请求。

还有

它们仅在发出请求时启动。如果长时间没有请求,就会关闭。

我会冒昧地猜测您的网络应用程序在一夜之间没有收到任何请求,关闭工作进程,当您的员工早上上班时,您看到的错误的第一人称在事件日志中?

也许值得查看特定应用的配置,或者每天早上 5 点运行计划任务以重新启动服务?

【讨论】:

  • 这很有趣,我一定会测试的!
猜你喜欢
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
相关资源
最近更新 更多