0

I have a problem with converting results from Invoke-Command into csv type. Code below :

    $remoteResultEventLog += Invoke-Command -ScriptBlock {
Param ( [string]$EventLogName,
        [string]$EventLogEntryType,
        [DateTime]$EventLogAfter, 
        [DateTime]$EventLogBefore, 
        [string] $searchPattern, 
        [Boolean] $caseSensitive)

    $events = Get-EventLog -LogName $EventLogName -EntryType $EventLogEntryType -After $EventLogAfter -Before $EventLogBefore
    if ( $caseSensitive ) 
    { 
        $events | Select-Object Index, Time, EntryType, Source, InstanceID, Message, PSComputerName, @{Name='Search Pattern';Expression={$searchPattern}} `
        | Where-Object { Select-String -InputObject $_.message -Pattern $searchPattern -Quiet -CaseSensitive}
    }
    else 
    { 
        $events | Select-Object Index, Time, EntryType, Source, InstanceID, Message, PSComputerName, @{Name='Search Pattern';Expression={$searchPattern}} `
        | Where-Object { Select-String -InputObject $_.message -Pattern $searchPattern -Quiet }
    } 
    } -ArgumentList $EventLogName, $EventLogEntryType, $EventLogAfter, $EventLogBefore, $searchPattern, $caseSensitive -Session $serverSession

    $remoteResultEventLogCsv = $remoteResultEventLog | ConvertTo-Csv -NoTypeInformation -Delimiter ";"

The last line give this error :

ConvertTo-Csv : Cannot bind argument to parameter 'InputObject' because it is null.

At C:\Temp\RemoteCheckTest\new\RemoteCheckFiles.ps1:131 char:65

+ $remoteResultEventLogCsv = $remoteResultEventLog | ConvertTo-Csv <<<<  -NoTypeInformation -Delimiter ";"

    + CategoryInfo          : InvalidData: (:) [ConvertTo-Csv], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToCsvCommand

But Input Object in that case $remoteResultEventLog is not a null I checked

Thanks for any answer

E235
  • 11,560
  • 24
  • 91
  • 141
Zabaa
  • 327
  • 1
  • 4
  • 16
  • 1
    What is the final backtick for on the `-ArgumentList` line? If you format your code with the pipe on the line preceding `command | other command | etc` the escape characters are not necessary – Eris Sep 02 '13 at 06:48
  • Sorry, maybe when i paste the code here that backtick shown. I edit and erase it. – Zabaa Sep 02 '13 at 07:04
  • Code works for me when the remote invocation returns objects. Did you double-check that the pattern actually produces matching results? – Ansgar Wiechers Sep 02 '13 at 11:44
  • 1
    Side note: you don't need the conditional in your script block. Simply pass the `$caseSensitive` argument to the `-CaseSensitive` switch: `Select-String -InputObject $_.message -Pattern $searchPattern -Quiet -CaseSensitive:$caseSensitive`. – Ansgar Wiechers Sep 02 '13 at 11:46
  • I check again and $remoteResultEventLog is not null for sure. Thanks Ansgar i will use that tip :) – Zabaa Sep 02 '13 at 21:01

1 Answers1

1

What's the content of $remoteResultEventLog? Maybe you could try to replace the last line in your script with:

$remoteResultEventLogCsv = $remoteResultEventLog | Where-Object {$_} | ConvertTo-Csv -NoTypeInformation -Delimiter ";"
David Brabant
  • 41,623
  • 16
  • 83
  • 111