【发布时间】:2014-12-16 01:33:58
【问题描述】:
我目前正在学习 powershell,并且正在尝试使用凭据从多个服务器(仅限错误和警告)获取系统和应用程序事件日志。对于此脚本中的其他内容,我已经能够将 CimSession 与 Get-CimInstance 一起使用,但它似乎不适用于 Win32_NTLogEvent。我也尝试过查看 Get-WinEvent,但不确定如何将其过滤为仅错误和警告。这基本上取代了以前的 Get-Eventlog,除了现在需要凭据。
之前的代码:
Get-Eventlog -LogName System -ComputerName $server -EntryType error,warning -Newest 20
Get-Eventlog -Logname Application -ComputerName $server -EntryType error,warning -Newest 20
目前的尝试:
如前所述,我不知道如何使用 Get-WinEvent 过滤 Get-Eventlog 中的条目类型。
Get-WinEvent -LogName System -ComputerName $server -Credential $cred
而使用 Get-CimInstance 我不能使用 CimSession
Get-CimInstance -ClassName Win32_NTLogEvent`
-filter 'type="error" or type="warning" and (logfile="system" or logfile="application")'`
|Select-Object -first 20
这是因为 Win32_NTLogEvent 来自 WMIObject 还是有其他原因?还有什么我可以用的吗?还是有什么我应该将 Get-WinEvent 导入的东西?
【问题讨论】:
-
好吧,我找到了解决方案,方法是使用 powershell 会话运行 get-eventlog。虽然如果有人有任何答案,我仍然会对可以做什么感兴趣。
-
顺便说一句,我是新来的,如果您要投反对票,我将不胜感激,您至少可以体面地解释原因。您让刚接触该网站的人对它产生了相当负面的看法。
-
你的第一个例子对我有用
Get-Eventlog -Logname Application -computer "localhost" -EntryType error -Newest 20 -
好吧,当我将它用于远程服务器时,它不能单独工作。虽然通过 PSSession 使用它。
$PSS= New-PSSession -computername $server -credential $cred (new line) Invoke-command -session $PSS -scriptblock { Get-Eventlog -Logname Application -EntryType error,warning -Newest 20}也感谢任何对这个问题投反对票的人,我非常感谢。
标签: powershell powershell-3.0 event-log