PowerShell:拖尾日志文件,并将结果发送到Windows系统事件日志

时间:2014-05-22 23:06:41

标签: windows powershell

我尝试使用PowerShell在Windows 2k8中尾随日志文件,然后将其发送到Windows事件查看器。

我知道powershell可以' tail'运行日志:

Get-Content -Path C:\logs\events.log -Wait

这将实时拖尾日志(类似于linux尾部的-f)。

Powershell还可以写入Windows事件系统日志:

Write-EventLog System -source System -eventid 12345 -message "test"

我想知道如何(或者如果)我可以将尾部日志结果作为Write-EventLog消息进行管道传输?

我认为既然这是一个Windows环境,可能需要编写脚本来调用取自Get-Content的消息变量?有谁知道怎么做,或者是否可以做到这一点?我的谷歌搜索没有引导我进行实时Windows事件记录。

1 个答案:

答案 0 :(得分:4)

你可以这样做:

function Write-EventlogCustom($msg) {
    Write-EventLog System -source System -eventid 12345 -message $msg
}

Get-Content -Path C:\logs\events.log -Wait | % {Write-EventlogCustom $_}

你不一定需要创建一个函数,我只是用它来使代码更清晰。

相关问题