powershell - 写入事件日志以查找错误

标签 powershell events

运行以下一个类轮时:

Write-EventLog -LogName Application -Source 'Application Error' -EntryType Error -EventID 1001 -Message 'Problem description'

我们在日志Application中看到条目:

enter image description here

根据Microsoft对于 EventID 1001,应提供 %1、%2 和 %3 的值:

Detection of product '%1', feature '%2' failed during request for component '%3'

这在 PowerShell 中怎么可能实现?添加开关 -RawData 10, 20 时,仅填写类型,如下所示:

enter image description here

有没有办法在事件日志中不创建新的日志名称或源的情况下不使用其他文本?或者能够填写变量?我正在写应用程序错误,以防自定义日志名称或源不可用。所以某处有痕迹。

感谢您的帮助。

最佳答案

您链接到的事件 ID 1001 描述特定于 MsiInstaller 源。

对于您自己的自定义错误事件,请使用自定义源标识符。您可以检查机器上是否已存在源定义,如果不存在,则创建它:

$CustomSource = 'MyCustomApplication'

# Wrap check i try/false to catch SourceExists() throwing access denied on failure
$SourceExists = try {
    [System.Diagnostics.EventLog]::SourceExists($CustomSource)
} catch {
    $false
}

if(-not $SourceExists)
{
    # Create the Source definition
    New-EventLog -Source $CustomSource -LogName Application
}

Write-EventLog -LogName Application -Source $CustomSource -EntryType Error -EventID 1001 -Message 'Problem description'

如果您有消息资源文件(包含事件"template"的文件),您还可以将其包含在对 New-EventLog 的调用中

关于powershell - 写入事件日志以查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355610/

相关文章:

powershell - 在 powershell 中使用另一个 cmdlet 的参数定义?

windows - 等效于 cURL 命令上传文件的 PowerShell

powershell - 获取当前目录下子文件夹中文件的相对路径

c# - 我的 ViewModel 知道 View,我该如何解决这个问题?

c# - 如何立即触发 timer.Elapsed 事件

javascript - 更改检测在 Angular 2 的指令事件输出中不起作用

c# - WPF 窗口关闭事件用法

powershell - 将PowerShell命令转换为脚本

asp.net - 从 .NET 调用父 ps1 时如何执行子 ps1

javascript - 事件冒泡是如何工作的以及它是什么?