【发布时间】:2018-11-10 17:54:47
【问题描述】:
我正在尝试根据来自我的 cmdlet 的流将事件日志输出到正确的条目类型(信息、警告、错误),如下所示:
function myfunction {
Param(
[switch]$stream1,
[switch]$stream2
)
if ($stream1) {write-output 'stream 1 msg'}
if ($stream2) {write-error 'stream 2 msg'}
}
$eventlogparams = @{'logname'='application';'source'='myapp';'eventid'='1'}
myfunction -stream1 -stream2 `
1> write-eventlog @eventlogparams -entrytype information -message $_ `
2> write-eventlog @eventlogparams -entrytype error -message $_
有没有人知道如何做到这一点?
【问题讨论】:
-
您需要在目标系统上命名为
myapp的source。使用New-Eventlogcmdlet 及其-Source参数创建。看看这篇文章... 如何使用 PowerShell 写入事件日志 – 嘿,脚本专家!博客 — blogs.technet.microsoft.com/heyscriptingguy/2013/06/20/… -
@Lee_Dailey:我认为
Write-EventLog参数只是示例。如果我理解正确,OP 的愿望是处理来自 all 流(或至少来自成功和错误流)的输出,并能够区分输入来自哪个流,以便行为可以相应地调整(在这种情况下选择适当的事件类别)。 -
@mklement0 - 这是有道理的。 [grin] 我仍然不确定 OP 是否知道
Source必须首先存在。这是我第一次尝试写入自定义事件日志时看到人们提到的最常见的错误。 -
我已经知道需要事先创建源
-
@ErikW - 感谢您澄清这一点! [咧嘴]
标签: powershell event-log io-redirection