【问题标题】:Powershell Write-EventLog / Get-WinEvent Message issuesPowershell Write-EventLog / Get-WinEvent 消息问题
【发布时间】:2014-09-26 06:21:30
【问题描述】:

第一个命令在事件日志中创建一个条目,它似乎正在工作,因为我可以在事件查看器中看到消息数据。问题是当从 powershell 读取它时,消息字段为空。

write-eventlog System -source 'Microsoft-Windows-Kernel-General' -eventid 999 -message 'Kernel something or other'

get-winevent -filterHashTable @{Logname = 'System'; ID = '999'}| select-object -first 10

也许这张图片能更好地解释它。请注意消息列是空白的。

【问题讨论】:

  • 我不知道原因,但这应该有效:Get-EventLog -LogName System -InstanceId 999 | Select-Object -First 10

标签: powershell powershell-2.0 powershell-3.0 event-log powershell-4.0


【解决方案1】:

事件正在正确写入,要读回它,请使用:

get-winevent -filterHashTable @{Logname = 'System'; ID = '999'}| 
    select-object -first 10 | select timecreated,providername,
    @{n="Message";e={$_.properties.Value}}

启动eventvwr时看不到消息列的原因很明显:

找不到来自源 Microsoft-Windows-Kernel-General 的事件 ID 999 的描述。引发此事件的组件未安装在本地计算机上,或者安装已损坏。您可以在本地计算机上安装或修复组件。

如果您想从自定义来源编写自定义消息,请使用 New-EventLog cmdlet,这里是脚本专家的教程:http://blogs.technet.com/b/heyscriptingguy/archive/2013/06/20/how-to-use-powershell-to-write-to-event-logs.aspx

【讨论】:

    【解决方案2】:

    这是最终使它起作用的片段。感谢 Raf 提供我找到此答案的链接。

    $source = "Some Name"
    If ([System.Diagnostics.EventLog]::SourceExists("$source") -eq $false)
    {New-EventLog -LogName $log -Source "$source"}
    

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2019-08-09
      • 2017-08-06
      • 2022-11-18
      • 1970-01-01
      相关资源
      最近更新 更多