0

我在将结果转换Invoke-Command为 csv 类型时遇到问题。下面的代码:

    $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 ";"

最后一行给出了这个错误:

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

但是在这种情况下输入对象$remoteResultEventLog不是我检查的空值

感谢您的任何回答

4

1 回答 1

1

$remoteResultEventLog 的内容是什么?也许您可以尝试将脚本中的最后一行替换为:

$remoteResultEventLogCsv = $remoteResultEventLog | Where-Object {$_} | ConvertTo-Csv -NoTypeInformation -Delimiter ";"
于 2013-09-02T09:13:51.460 回答