[英]Write-Eventlog pipe issue
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. 这个小的CSV存储在D:\\ Powershell \\ Bmc.log中,几乎是服务器上ipmitool.exe
( isel -c
)的解析输出。
$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. 这很好用,我可以将$sel
用作对象,可以列出其成员等。现在我想将所有记录转储到eventlog,但是它不起作用。 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. 似乎Write-Eventlog
不接受管道。 Aim is not to use a loop. 目的是不使用循环。
As you suspected and others have pointed out, Write-EventLog
doesn't read from the pipeline. 正如您怀疑和其他人指出的那样, Write-EventLog
不会从管道中读取。 Wrap it in a ForEach-Object
statement: 将其包装在ForEach-Object
语句中:
$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: 否则,您需要将Write-EventLog
包装在执行处理管道输入的自定义函数中:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.