write-eventlog不写入应用程序日志(在注册首选源之后)

时间:2017-01-23 19:17:58

标签: powershell event-viewer

我尝试写入eventlog“application”。我使用了ScriptingGuy-Tutorial。如博客中所述,我在“以管理员身份运行”中使用以下命令注册源“ScS”-Powershell: New-EventLog -logname Application -source "ScS"

我没有收到错误或任何其他反馈。在注册表中,我可以在路径中看到源

HKLM\System\CurrentControlSet\Services\EventLog\Application\ScS  

enter image description here

之后,我尝试写一个事件:

write-eventlog -logname Application -Source "ScS" -EntryType Information -EventId 01 -Message "Test"

我没有收到错误。但它不会写这个事件。我没有在eventvwr和shell中看到它,使用get-eventlog -LogName Application -Source "ScS"

我在Winershell 4.0中使用Win 8.1 Pro(德语)。希望你能告诉我我的错误......

2 个答案:

答案 0 :(得分:0)

您正在忽略第一个调用中的logname是您要创建的日志的名称" Application"是现有的日志。

以下是一个示例(PowerShell V2有自己的cmdlet):

# List of logs
Get-EventLog -list

# Creating your own log
New-EventLog -LogName "SlxScripting" -Source "MaSource"

# List of logs
Get-EventLog -list

# Writting in your own log
Write-EventLog -LogName "SlxScripting" -EventId 12 `
               -Message "Mon Message" 
               -Source "MaSource" -EntryType Warning

# Reading (consuming) in your own log
Get-EventLog -LogName "SlxScripting"  

# Suppressing your log (if needed)
Remove-EventLog -LogName "SlxScripting"

答案 1 :(得分:0)

我在 Server 2012 R2 上遇到了同样的问题。如链接的脚本专家教程中所示,应该可以将源添加到现有应用程序日志中——无需创建自己的日志。

我在 this StackOverflow answer 中发现了一个提示:停止并重新启动 Windows 事件日志服务。一旦我这样做了,Write-EventLog 开始在应用程序事件日志中创建事件。我还必须重新启动事件查看器以消除一些警告,例如“找不到描述”。根据 this article 的底部,这是由于事件查看器缓存。其他如 {​​{3}} 建议只需重新启动计算机。

请注意,在 Windows 10 中不需要重新启动服务。这似乎是 Windows 8.1 / 2012 R2 的错误。