简体   繁体   English

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

[英]PowerShell Write-EventLog Not Writing to Desired Event Log

I have the below code (snippet) in a PowerShell function (named Create-EventLog).我在 PowerShell 函数(名为 Create-EventLog)中有以下代码(片段)。 The end result should be the following:最终结果应该如下:

  • A new Event Log named Get-Drives were PowerShell writes all exception messages for this application.一个名为Get-Drives新事件日志被 PowerShell 写入此应用程序的所有异常消息。 The Get-Drives event log gets created but for some reason the actual message(s) are only getting written in the default Windows PowerShell event log. Get-Drives事件日志已创建,但由于某种原因,实际消息仅写入默认的Windows PowerShell事件日志。 How can I make PowerShell write the messages to the "Get-Drives" event log instead of the "Windows PowerShell" event log?如何让 PowerShell 将消息写入“Get-Drives”事件日志而不是“Windows PowerShell”事件日志? I'm using PowerShell 4 and using PowerShell tools in Visual Studio 2013.我正在使用 PowerShell 4 并在 Visual Studio 2013 中使用 PowerShell 工具。

My PowerShell function snippet:我的 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
}

Running Get-EventLog -List reveals the below output:运行Get-EventLog -List显示以下输出:

在此处输入图片说明

Under entries there is a value 1 for the Get-Drives log but it only displays in the Windows Powershell log which also has a value of 1.在条目下, Get-Drives日志的值为 1,但它仅显示在 Windows Powershell 日志中,该日志的值为 1。

Screenshot of the Event Viewer事件查看器的屏幕截图

Get-Drives log:获取驱动器日志: 在此处输入图片说明

Windows Powershell log: Windows Powershell 日志: 在此处输入图片说明

Let's see if your EventLog is created, this command will list all your events log's on local computer:让我们看看您的 EventLog 是否已创建,此命令将列出本地计算机上的所有事件日志:

Get-EventLog -list获取-EventLog -list

If your EventLog is not in the list, try to create it by using just this command and not your function(run as administrator your PS console):如果您的 EventLog 不在列表中,请尝试仅使用此命令而不是您的功能来创建它(以管理员身份运行您的 PS 控制台):

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

Then run again to see if your EventLog has been created.然后再次运行以查看您的 EventLog 是否已创建。

Get-EventLog -list获取-EventLog -list

If your new EventLog present in the list then run your function to see if now he will be able to write in the right EventLog如果您的新 EventLog 出现在列表中,则运行您的函数以查看他现在是否能够写入正确的 EventLog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM