Reputation: 21
Task: Writing piped data from object to eventlog:
RecId|Date/Time|Sev|Src|EvtType|Sensor|EvtDetail 0001|01/01/1600:00:00|INF|BMC|Eventlog|SELFullness|LogCleared 0002|01/01/1600:00:01|MAJ|BMC|Eventlog|Progress|PostError 0003|01/01/1600:00:02|INF|BMC|PowerUnit|HostPower|PowerOff 0004|01/01/1600:00:03|MAJ|BMC|SystemFirmware|SELFullness|PostError 0005|01/01/1600:00:04|INF|BMC|OsBoot||C:bootcompleted
This little CSV is stored in D:\Powershell\Bmc.log is nearly a parsed output of ipmitool.exe
(isel -c
) on a server.
$raw = Get-Content .\Bmc.log
$sel = ConvertFrom-CSV -InputObject $raw -Delimiter "|"
$sel | Where-Object {$_.Sev -eq "INF"} | Out-GridView
This works well, I can use $sel
as object, can list its members etc.
Now I want to dump all records to eventlog, but it does not work.
My Approach:
New-Eventlog -LogName HardwareEvents -Source BMC
$sel | Where-Object {$_.Sev -eq "INF"} | Write-Eventlog -LogName HardwareEvents -Source BMC -EventId 999 -Message {$_.EvtDetail} -EntryType INFORMATION
It seems that Write-Eventlog
does not accept pipes.
Aim is not to use a loop.
Upvotes: 0
Views: 467
Reputation: 200503
As you suspected and others have pointed out, Write-EventLog
doesn't read from the pipeline. Wrap it in a ForEach-Object
statement:
$sel | Where-Object {$_.Sev -eq "INF"} | ForEach-Object {
Write-EventLog -LogName HardwareEvents -Source BMC -EventId 999 -Message $_.EvtDetail -EntryType INFORMATION
}
Otherwise you'd need to wrap Write-EventLog
in a custom function that does process pipeline input:
function Write-EventLogMessage {
[CmdletBinding()]
Param(
[Parameter(
Mandatory=$true,
Position=0
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true
)]
[string]$Message
)
Process {
Write-EventLog -LogName HardwareEvents -Source BMC -EventId 999 -Message $Message -EntryType INFORMATION
}
}
$sel | Where-Object {$_.Sev -eq "INF"} | Write-EventLogMessage
Upvotes: 1