2012-04-13 75 views
0

我想运行PowerShell脚本并尝试按消息过滤。PowerShell错误使用GetEventLog CmdLet

param($server,$message) 
Try 
{ 
Invoke-Command -computername $server {Get-Eventlog -logname application -source "source" -message $message | Format-List} 
} 
Catch [Exception] 
{ 
Write-Host $_.Exception.ToString() 
} 

试图用下面的参数

运行脚本GetEventLog.ps1 “服务器名称”, “TEXT_TO_FIND”

无法验证的参数 '消息' 的说法。参数为空或空。提供一个非空的参数或 为空,然后再次尝试该命令。 + CategoryInfo:InvalidData:(:) [获取,事件日志],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

出于某种原因,处理$服务器参数正常,但如果抱怨有关$消息变量。

任何线索?

+0

Get-EventLog太慢了!!我结束使用这个.. Get-WinEvent -computername $ server -FilterHashTable @ {LogName ='application'; providername = $ provider} | Where-Object {$ _。Message -match $ message -And $ _。TimeCreated -ge $ after -And $ _。TimeCreated -le $ before} – Maverick 2012-04-18 19:48:12

回答

0

您需要使用-ArgumentList参数将$ message作为参数传递。 CHECK OUT例如9手册页:

http://technet.microsoft.com/en-us/library/dd347578.aspx

invoke-command -computername server01 -scriptblock {param($log, $num) get-eventlog -logname $log -newest $num} -ArgumentList $MWFO_Log, 10 
+0

必须尝试此操作,但绝对不像Shay Levy那样清晰和简单。为什么我需要使用scriptblock ?.我想如果我不使用invoke-command,那么我不需要这种方法。 :) – Maverick 2012-04-13 20:02:18

+0

@Maverick是的,它恰好如此,Get-EventLog cmdlet已经支持远程执行,所以不需要使用Invoke-Command。 – 2012-04-13 20:05:13

1

试试这个方法:

Invoke-Command -computername $server {Get-Eventlog -logname application -source "source" -message $args[0] | Format-List} -ArgumentList $message 
+0

这没有奏效 – Maverick 2012-04-13 20:01:00

+0

你有没有同样的错误? – 2012-04-13 20:05:53

+0

这不是@ShayLevy的解决方案,但无论如何,它解决了您发布的错误。 – 2012-04-13 20:41:00

3

为什么使用调用命令时,你可以得到的事件没有它?

Get-EventLog -ComputerName $server -LogName application -source "source" -message $message 

万一命令生成您将无法赶上它,因为它可能不会终止错误的错误。要使错误终止,请使用ErrorAction参数:

Get-EventLog -ComputerName $server -LogName application -ErrorAction Stop ... 
+0

好电话谢伊! – 2012-04-13 19:52:29

+0

谢谢@AndyArismendi :) – 2012-04-13 19:57:45

+0

这实际上工作! – Maverick 2012-04-13 20:00:43