PowerShell in error using GetEventLog CmdLet

I am trying to run a PowerShell script and try to filter by the message.

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

I am trying to run the script with the following parameters:

GetEventLog.ps1 "SERVERNAME" "TEXT_TO_FIND"

cannot validate an argument on parameter 'Message'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. + CategoryInfo : InvalidData: (:) [Get-EventLog], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetEventLogCommand

For some reason it handles the $server parameter fine, but if complains about the $message variable.

How can I fix this?

Answers


You can get the events without Invoke-Command:

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

In case the command generates an error you won't be able to catch it since it probably will not be a terminating error. To make the error terminating, use the ErrorAction parameter:

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

Try this way:

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

You'll need to pass $message as an argument using the -ArgumentList parameter. Check out example 9 of the man page:

Invoke-Command

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

Need Your Help

"not connected" in android bluetooth application

android eclipse bluetooth emulation

I want to run a bluetooth chat application (sample code here).

Event handlers not working in xaml

c# wpf xaml event-handling

I have a TextBox in my page. That has INotifyChangeProperty