【问题标题】:PowerShell in error using GetEventLog CmdLetPowerShell 使用 GetEventLog CmdLet 出错
【发布时间】:2012-04-26 04:40:55
【问题描述】:

我正在尝试运行 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 或空。提供一个不为空的参数或 清空,然后再次尝试该命令。 + CategoryInfo : InvalidData: (:) [Get-EventLog], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

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

我该如何解决这个问题?

【问题讨论】:

  • Get-EventLog 太慢了!!我结束了使用这个.. Get-WinEvent -computername $server -FilterHashTable @{LogName='application';providername=$provider} | Where-Object {$_.Message -match $message -And $_.TimeCreated -ge $after -And $_.TimeCreated -le $before}

标签: powershell cmdlet


【解决方案1】:

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

Invoke-Command

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

【讨论】:

  • 必须尝试一下,但绝对不如 Shay Levy 的回答那么清晰和简单。为什么我需要使用脚本块?我想如果我不使用调用命令,那么我就不需要这种方法。 :)
  • @Maverick 是的,Get-EventLog cmdlet 恰好支持远程执行,因此无需使用 Invoke-Command。
【解决方案2】:

试试这个方法:

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

【讨论】:

  • 这不是@ShayLevy 的正确解决方案,但无论如何它解决了您发布的错误。
【解决方案3】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2021-09-30
    • 2021-10-12
    相关资源
    最近更新 更多