2

我正在创建一个新的 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'.

4

1 回答 1

2

据我所知,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)  
} 
于 2013-07-07T09:46:46.423 回答