我在一个PowerShell函数(名为Create)中有下面的代码(片段)。最终结果应如下:
Get-Drives
的新事件日志PowerShell写入此应用程序的所有异常消息。将创建Get-Drives
事件日志,但由于某种原因,实际消息只会在默认的Windows PowerShell
事件日志中写入。如何使PowerShell将消息写入“Get-驱动器”事件日志而不是“PowerShell”事件日志?我在Visual 2013中使用PowerShell 4和PowerShell工具。我的PowerShell函数片段:
if (!$EventLogExists) {
New-EventLog -LogName "Get-Drives" -source "Get-Drives"
Write-EventLog -LogName "Get-Drives" -source "Get-Drives" -EventId 1 -message "There was an error"
return
} else {
Write-EventLog -LogName "Get-Drives" -source "Get-Drives" -EventId 1 -message "There was an error"
return
}
运行Get-EventLog -List
将显示以下输出:
在条目下,Get-Drives
日志有一个值1,但它只显示在Windows日志中,该日志的值也是1。
事件查看器的屏幕截图
获取-驱动器日志:
Windows Powershell日志:
发布于 2015-10-25 08:07:06
让我们看看是否创建了EventLog,这个命令将列出本地计算机上的所有事件日志:
Get-EventLog -list
如果您的EventLog不在列表中,请使用以下命令来创建它,而不是使用您的函数(以管理员身份运行PS控制台):
新EventLog -LogName“Get-驱动器”-source获取-驱动器
然后再运行一次,查看您的EventLog是否已经创建。
Get-EventLog -list
如果您的新EventLog出现在列表中,那么运行您的函数,看看他现在是否能够在正确的EventLog中编写
发布于 2022-11-09 08:52:42
我也面临着这个问题;这似乎是EventViewer中的一个问题。我修复了它,重新启动Windows事件日志服务和重新创建de EventSource。
https://stackoverflow.com/questions/33331133
复制