0

我正在尝试运行 PowerShell 脚本并尝试按消息过滤。

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 "SERVERNAME" "TEXT_TO_FIND"

无法验证参数“消息”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后再次尝试该命令。+ CategoryInfo : InvalidData: (:) [Get-EventLog], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

由于某种原因,它可以很好地处理 $server 参数,但如果抱怨 $message 变量。

我怎样才能解决这个问题?

4

3 回答 3

3

您可以在没有 Invoke-Command 的情况下获取事件:

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

如果命令生成错误,您将无法捕获它,因为它可能不是终止错误。要使错误终止,请使用 ErrorAction 参数:

   Get-EventLog -ComputerName $server -LogName application -ErrorAction Stop ...
于 2012-04-13T19:50:59.630 回答
1

试试这个方法:

Invoke-Command -computername $server {Get-Eventlog -logname application -source "source" -message $args[0] | Format-List} -ArgumentList $message
于 2012-04-13T19:43:39.333 回答
0

您需要使用 -ArgumentList 参数将 $message 作为参数传递。查看手册页的示例 9:

调用命令

invoke-command -computername server01 -scriptblock {param($log, $num) get-eventlog -logname $log -newest $num} -ArgumentList $MWFO_Log, 10
于 2012-04-13T19:39:26.810 回答