下面的程序在本地计算机上运行良好,但当我将-ComputerName "myRemoteName“放入时,它会挂起,甚至在5分钟后也不会返回任何内容;但是程序似乎仍然在运行。
它是否试图通过“连线”返回一大包数据?理论上说,在过去的2小时里,我在远程计算机上应该有10个错误。
$getEventLog = Get-EventLog -log application -ComputerName "myRemoteName" -after ((get-date).addMinutes($minutes*-1)) -EntryType Error
Write-Host Get-Eventlog completed
# list of events to exclude (based on text found in the message)
$getEventLogFiltered = $getEventLog | Where-Object {$_.Message -notlike 'Monitis*' -and $_.Message -notlike '*MQQueueDepthMonitor.exe*' -and $_.Message -notlike '*The local computer may not have the necessary registry*' }
#to only select certain columns, use Select-Object -Property and list the property/columns
$getEventLogColumns = $getEventLogFiltered | Select-Object -Property TimeGenerated,Source,Message,EntryType,MachineName,EventID
$tableFragment = $getEventLogColumns | ConvertTo-Html -fragment
Write-Host "HTML-Table Built"然后生成一封电子邮件然后发送出去..。
我看过其他的帖子,建议改用Get-WinEvents,但我想这需要一两个小时才能重写(因为我对Powershell缺乏经验);我上面的内容是在本地计算机上很好地工作。
Updates 03/04/2014 13:40 CT:
Running with $minutes = 120 ran 14.5 minutes.
Running with $minutes = 1 ran 12.5 minutes. 结论:改变$minutes的范围并不真正影响反应时间,两者都是缓慢的。
发布于 2015-04-01 14:52:11
我似乎错了,即使使用Where对象过滤器,它仍然像在-after参数的情况下那样扫描(我只是在不同的新构建的机器上测试,这就是为什么它完成得这么快)。
然而,更多的研究表明,中断函数可能是有用的,所以我所做的是:
Get-EventLog -ComputerName $ServerName -LogName Security | WHERE { ($_.EventID -eq '528')} | ForEach-Object {
$_
if ($_.TimeGenerated.CompareTo($YDate) -lt 1) { Break}
}它打印所有的事件日志,当它击中事件日志时(在我的例子中是24小时),中断启动并停止。
这不是最漂亮的解决方案,但到目前为止似乎还不错。
发布于 2015-03-31 08:28:02
在参数没有很好的设计之后,它打印它应该打印的所有记录,但是当它到达设定的日期时,它仍然扫描到偶数日志文件的末尾,尽管事实是没有什么可打印的(至少看起来是这样的)。我已经使用Where过滤器和.CompareTo()方法打印了在设定日期之后的日志(在我的例子中,是当前日期的前一天)。
#Sets yesterday date (script will get all records after that date)
$YDate = (Get-Date).AddDays(-1)
#Gets all event logs from Security log where event id represents successful logon and records where generated after prepared date (current date - 24 hours)
$YestardayLogons = Get-EventLog -ComputerName $ServerName -LogName Security |
WHERE { ($_.EventId -eq '528') -and ($_.TimeGenerated.CompareTo($YDate) -eq '1') }https://stackoverflow.com/questions/22175885
复制相似问题