使用 Powershell,如果最近的事件日志事件超过一小时,则触发操作

Jef*_*ert 4 windows powershell windows-event-log

我们有一台服务器运行我们的备份软件,该软件有自己的事件日志。我可以使用以下命令检索最新的事件日志条目:

Get-EventLog EventLogName -ComputerName server.example.com -newest 1
Run Code Online (Sandbox Code Playgroud)

这给了我这样的结果:

Index Time          EntryType   Source                InstanceID Message
----- ----          ---------   ------                ---------- -------
64292 Aug 13 15:51  Information BackupSoftware             29593 Transfer of 1096 KB...
Run Code Online (Sandbox Code Playgroud)

如果最近事件的时间戳超过一小时,我想做的是触发一个操作(例如,启动第二个脚本)。

任何帮助,将不胜感激。

Rya*_*ies 5

$Event = Get-EventLog Application | ? { $_.Source -EQ 'BackupSoftware' } | Sort Time | Select -Last 1
If($Event.Time -LT (Get-Date).AddHours(-1)) 
{ 
     Do-Stuff 
}
Run Code Online (Sandbox Code Playgroud)

这将在应用程序日志中找到源为“BackupSoftware”的最新事件。

$Event = Get-EventLog BackupSoftware | Sort Time | Select -Last 1
If($Event.Time -LT (Get-Date).AddHours(-1)) 
{ 
     Do-Stuff 
}
Run Code Online (Sandbox Code Playgroud)

这将在名为 BackupSoftware 的自定义日志中查找最新事件,而不管来源或 EventId。

在这两种情况下,Do-Stuff如果事件超过一个小时,脚本就会执行。