Powershell Write-EventLog / Get-WinEvent消息问题

时间:2014-09-26 06:21:31

标签: powershell powershell-v2.0 powershell-v3.0 event-log powershell-v4.0

第一个命令在事件日志中创建一个条目,它似乎正在工作,因为我可以在事件查看器中看到消息数据。问题是当从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

也许这张照片解释得更好。请注意,消息列为空。 enter image description here

2 个答案:

答案 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

答案 1 :(得分:1)

这是最后使其工作的剪辑。感谢Raf找到了这个答案的链接。

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