在 Powershell 中创建 PerformanceCounterCategory

发布于 2024-09-16 22:54:16 字数 816 浏览 3 评论 0原文

我正在尝试在 powershell 中运行以下脚本:

$counters = @()

$counters = $counters + [Diagnostics.CounterCreationData]::("Hit counter", "Number of total hits", [Diagnostics.PerformanceCounterType]::NumberOfItem32);
$counters = $counters + [Diagnostics.CounterCreationData]::("Hits per second", "Number of average hits per second", [Diagnostics.PerformanceCounterType]::RateOfCountsPerSecond32);

$counterCollection = [Diagnostics.CounterCreationDataCollection]::($counters);

[Diagnostics.PerformanceCounterCategory]::Create("HitCounters","Some help text",[Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $counterCollection);

当我执行此脚本时,我收到一条错误消息,指出 $counterCollection 为 null。 恐怕我对 powershell 还不够熟悉,无法找出问题所在 - 这是我构建集合的数组吗?或者 CounterCreationDataCollection 创建调用本身?

非常欢迎任何指点:)

I'm trying to run the following script in powershell:

$counters = @()

$counters = $counters + [Diagnostics.CounterCreationData]::("Hit counter", "Number of total hits", [Diagnostics.PerformanceCounterType]::NumberOfItem32);
$counters = $counters + [Diagnostics.CounterCreationData]::("Hits per second", "Number of average hits per second", [Diagnostics.PerformanceCounterType]::RateOfCountsPerSecond32);

$counterCollection = [Diagnostics.CounterCreationDataCollection]::($counters);

[Diagnostics.PerformanceCounterCategory]::Create("HitCounters","Some help text",[Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $counterCollection);

When I execute this, I get an error saying $counterCollection is null.
I'm afraid I'm not yet familiar enough with powershell to sort out where this is going wrong - is it the array I'm building the Collection from? Or the CounterCreationDataCollection creation call itself?

Any pointers are more than welcome :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-09-23 22:54:16

您将静态访问器语法 :: 与构造函数调用混合在一起。试试这个:

$ccdTypeName = 'System.Diagnostics.CounterCreationDate'
$ccdcTypeName = 'System.Diagnostics.CounterCreationDataCollection'
$counters = @()
$counters += new-object $ccdTypeName "Hit counter","..."
$counters += new-object $ccdTypeName "Hits per sec","..."
$counterCollection = new-object $ccdcTypeName $counters
...

You're mixing in static accessor syntax :: with the constructor call. Try this instead:

$ccdTypeName = 'System.Diagnostics.CounterCreationDate'
$ccdcTypeName = 'System.Diagnostics.CounterCreationDataCollection'
$counters = @()
$counters += new-object $ccdTypeName "Hit counter","..."
$counters += new-object $ccdTypeName "Hits per sec","..."
$counterCollection = new-object $ccdcTypeName $counters
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文