【问题标题】:PowerShell: Tailing a log file, and sending results to the Windows system event logPowerShell:跟踪日志文件,并将结果发送到 Windows 系统事件日志
【发布时间】:2014-05-22 23:06:40
【问题描述】:

我正在尝试使用 powershell 在 Windows 2k8 中跟踪日志文件,然后将其发送到 Windows 事件查看器。

我知道 powershell 可以通过运行“跟踪”日志:

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

这将实时跟踪日志(类似于 linux tail 中的 -f)。

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

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

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

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

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    您可能可以这样做:

    function Write-EventlogCustom($msg) {
        Write-EventLog System -source System -eventid 12345 -message $msg
    }
    
    Get-Content -Path C:\logs\events.log -Wait | % {Write-EventlogCustom $_}
    

    你不一定需要做一个函数,我只是用它来让代码更干净。

    【讨论】:

    • 谢谢。我不是 100% 熟悉 powershell 语法,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 2019-07-17
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多