运行以下一个类轮时:
Write-EventLog -LogName Application -Source 'Application Error' -EntryType Error -EventID 1001 -Message 'Problem description'
我们在日志Application
中看到条目:
根据Microsoft对于 EventID 1001,应提供 %1、%2 和 %3 的值:
Detection of product '%1', feature '%2' failed during request for component '%3'
这在 PowerShell 中怎么可能实现?添加开关 -RawData 10, 20
时,仅填写类型,如下所示:
有没有办法在事件日志中不创建新的日志名称或源的情况下不使用其他文本?或者能够填写变量?我正在写应用程序错误
,以防自定义日志名称或源不可用。所以某处有痕迹。
感谢您的帮助。
最佳答案
您链接到的事件 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/