Powershell

使用 Powershell 獲取一組特定的性能計數器

  • November 17, 2020

我正在嘗試使用 powershell 設置一個腳本,該腳本每隔一定時間生成 blg 文件。我知道這可以使用 perfmon XML 模板來完成,但是對於這個特定的項目,如果可能的話,我必須使用 powershell 來完成。

我的主要問題是我無法將要使用的性能計數器列表儲存在變數中並重用它們。

我嘗試使用以下腳本創建要使用的性能計數器列表:

$Counters = @()
$Counters += '\Memory\Available Bytes' 
$counters += '\Paging File(*)\% Usage'
$Counters += '\PhysicalDisk(*)\Disk Reads/sec'
$Counters += '\PhysicalDisk(*)\Disk Writes/sec'
$Counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$Counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$Counters += '\Processor(*)\% Processor Time'
$Counters += '\System\Processor Queue Length'
foreach($counter in $Counters)
{
$string = $string + ", '" + $counter + "'" 
$string = $string.TrimStart(",")
}

如果我繼續使用 get-counter $string 我收到以下錯誤:

Get-Counter : The specified counter path could not be interpreted.

然而,當我複製字元串的確切值並使用*$string 的 get-counter -counter 值時,*它可以正常工作……

有人可以建議我讓get-counter使用數組或帶有計數器列表的字元串嗎?

當我使用你的+=塊時,我得到$counters一個長連接字元串。

$counters += '\Memory\Available Bytes'
$counters += '\Paging File(*)\% Usage'
$counters += '\PhysicalDisk(*)\Disk Reads/sec'
$counters += '\PhysicalDisk(*)\Disk Writes/sec'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$counters += '\Processor(*)\% Processor Time'
$counters += '\System\Processor Queue Length'
$counters

\Memory\Available Bytes\Paging File(*)\% Usage\PhysicalDisk(*)\Disk Reads/sec\PhysicalDisk(*)\Disk Writes/sec\PhysicalDisk(*)\Avg. Disk sec/Read\PhysicalDisk(*)\Avg. Disk sec/Write\Processor(*)\% Processor Time\System\Processor Queue Length

$counters.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

那可能不是你想要的。如果你$counters明確地製作一個數組,事情會更好一些。

$counters = @()
$counters += '\Memory\Available Bytes' 
$counters += '\Paging File(*)\% Usage'
$counters += '\PhysicalDisk(*)\Disk Reads/sec'
$counters += '\PhysicalDisk(*)\Disk Writes/sec'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$counters += '\Processor(*)\% Processor Time'
$counters += '\System\Processor Queue Length'
$counters.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

$counters | Get-Counter

引用自:https://serverfault.com/questions/625962