使用Powershell创建新的EventSource与C#不同

本文关键字:不同 EventSource Powershell 创建 使用 | 更新日期: 2023-09-27 18:21:11

我正在创建一个新的EventLog EventSource,并使用我自己的自定义类别集。

我目前在C#代码中使用以下代码来完成此操作:

var eventSourceData = new EventSourceCreationData(sourceName, logName)
                                      {
                                          CategoryCount = AllCategories.Count(),
                                          CategoryResourceFile = resourceFilePath,
                                          ParameterResourceFile = resourceFilePath,
                                          MessageResourceFile = resourceFilePath
                                      };
EventLog.CreateEventSource(eventSourceData);

我现在已经将其转换为如下的powershell脚本:

New-eventlog -logname $Log -ComputerName $ComputerName -Source $Source -CategoryResourceFile $categoryDllPath -MessageResourceFile $categoryDllPath -ErrorVariable Err -CategoryCount 20

Notr-CategoryCount 20最后,我的脚本在这个参数上失败了,说它不是一个有效的参数。(似乎不是)

所以我的问题是,使用Powershell,我如何提供CategoryCount,以便日志记录正常工作?

非常感谢。

错误消息为A parameter cannot be found that matches parameter name 'CategoryCount'.

使用Powershell创建新的EventSource与C#不同

据我所知,New-EventLog不支持CategoryCount。

但是您仍然可以直接在Powershell中使用.NET类,所以这样的东西应该可以工作:

$eventSourceData = new-object System.Diagnostics.EventSourceCreationData("$SourceName", "$logName")  
$eventSourceData.CategoryCount = 20
$eventSourceData.CategoryResourceFile = $CategoryDllPath
$eventSourceData.MessageResourceFile = $CategoryDllPath
If (![System.Diagnostics.EventLog]::SourceExists($eventSourceData.Source))
{      
[System.Diagnostics.EventLog]::CreateEventSource($eventSourceData)  
}