0
votes

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?

3
Get-EventLog is so slow!!. I ended using this.. Get-WinEvent -computername $server -FilterHashTable @{LogName='application';providername=$provider} | Where-Object {$_.Message -match $message -And $_.TimeCreated -ge $after -And $_.TimeCreated -le $before} - Maverick

3 Answers

3
votes

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 ...
1
votes

Try this way:

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

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