.net - PowerShell Write-EventLog 未写入所需的事件日志

标签 .net visual-studio powershell

我在 PowerShell 函数(名为 Create-EventLog)中有以下代码(片段)。最终结果应该如下:

  • 名为 Get-Drives 的新事件日志是 PowerShell 为该应用程序写入的所有异常消息。 Get-Drives 事件日志已创建,但出于某种原因 实际消息仅写入默认的 Windows PowerShell 事件日志。如何让 PowerShell 将消息写入“Get-Drives”事件日志而不是“Windows PowerShell”事件日志?我正在使用 PowerShell 4 并在 Visual Studio 2013 中使用 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 显示以下输出:

enter image description here

在条目下,Get-Drives 日志的值为 1,但它仅显示在 Windows Powershell 日志中,该日志的值也为 1。

事件查看器的屏幕截图

获取驱动器日志: enter image description here

Windows Powershell 日志: enter image description here

最佳答案

让我们看看您的 EventLog 是否已创建,此命令将列出您在本地计算机上的所有事件日志:

Get-EventLog -list

如果您的 EventLog 不在列表中,请尝试仅使用此命令而不是您的函数来创建它(以管理员身份运行您的 PS 控制台):

New-EventLog -LogName "Get-Drives" -source "Get-Drives"

然后再次运行,看看你的EventLog是否已经创建。

Get-EventLog -list

如果您的新事件日志出现在列表中,那么运行您的函数以查看他现在是否能够写入正确的事件日志

关于.net - PowerShell Write-EventLog 未写入所需的事件日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33331133/

相关文章:

c# - Azure:如何在表存储中保存 IDictionary<String>?

c# - 如何将 Razor View 转换为字符串?

c# - 在 WinRT 中编译并运行外部 Java 程序?

android - 如何更改 Android SDK 管理器的安装位置?

PowerShell:复制项目找不到路径

java - 负载测试 CMS Web 应用程序的实际度量是什么?

c# - 如何从 C 程序生成 DLL 以用于 C# 项目

visual-studio - 保持代码格式始终正确?

powershell - PowerShell 中的模糊字符串匹配

datetime - 如何在 PowerShell 中格式化日期时间