共用方式為


Get-Counter

從本機和遠端電腦取得性能計數器數據。

語法

Get-Counter
   [[-Counter] <String[]>]
   [-SampleInterval <Int32>]
   [-MaxSamples <Int64>]
   [-Continuous]
   [-ComputerName <String[]>]
   [<CommonParameters>]
Get-Counter
   [-ListSet] <String[]>
   [-ComputerName <String[]>]
   [<CommonParameters>]

Description

此 Cmdlet 只能在 Windows 平臺上使用。

Get-Counter Cmdlet 會直接從 Windows 作業系統家族中的效能監控設備取得效能計數器資料。 Get-Counter 從本機電腦或遠端電腦取得效能數據。

您可以使用 Get-Counter 參數來指定一或多部計算機、列出性能計數器集及其所包含的實例、設定取樣間隔,以及指定樣本數目上限。 如果沒有參數,Get-Counter 取得一組系統效能計數器的效能計數據。

許多計數器集合都受到訪問控制清單 (ACL) 的保護。 若要查看所有計數器集合,請使用 [以系統管理員身分執行] 選項 開啟 PowerShell。

此 Cmdlet 已在 PowerShell 7 中重新引入。

注意

性能計數器名稱已當地語系化。 此處顯示的範例會使用性能物件、計數器和實例的英文名稱。 在使用另一種語言的系統上,名稱會有所不同。 使用 Get-Counter -ListSet 命令來查看本地化的名稱。

範例

範例 1:取得計數器集合清單

這個範例會取得本機計算機的計數器集合清單。

Get-Counter -ListSet *

CounterSetName     : Processor
MachineName        : .
CounterSetType     : MultiInstance
Description        : The Processor performance object consists of counters that measure aspects ...
                     computer that performs arithmetic and logical computations, initiates ...
                     computer can have multiple processors.  The processor object represents ...
Paths              : {\Processor(*)\% Processor Time, \Processor(*)\% User Time, ...
PathsWithInstances : {\Processor(0)\% Processor Time, \Processor(1)\% Processor Time, ...
Counter            : {\Processor(*)\% Processor Time, \Processor(*)\% User Time, ...

Get-Counter 會使用具有星號 () 的 * 參數來取得計數器集合清單。 . 資料行中的點 () 代表本機電腦。

範例 2:指定 SampleInterval 和 MaxSamples

此範例會取得本機電腦上所有處理器的計數器數據。 數據會以兩秒間隔收集,直到有三個樣本為止。

Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 3

Timestamp                 CounterSamples
---------                 --------------
6/18/2019 14:39:56        \\Computer01\processor(_total)\% processor time :
                          20.7144271584086

6/18/2019 14:39:58        \\Computer01\processor(_total)\% processor time :
                          10.4391790575511

6/18/2019 14:40:01        \\Computer01\processor(_total)\% processor time :
                          37.5968799396998

Get-Counter 會使用 Counter 參數來指定計數器路徑 \Processor(_Total)\% Processor TimeSampleInterval 參數會設定兩秒間隔來檢查計數器。 MaxSamples 會判斷三個是檢查計數器的最大次數。

範例 3:取得計數器的連續樣本

此示例每秒為計數器獲取連續的樣本。 若要停止命令,請按 CTRL+C。 若要指定樣本之間的較長間隔,請使用 SampleInterval 參數。

Get-Counter -Counter "\Processor(_Total)\% Processor Time" -Continuous

Timestamp                 CounterSamples
---------                 --------------
6/19/2019 15:35:03        \\Computer01\processor(_total)\% processor time :
                          43.8522842937022

6/19/2019 15:35:04        \\Computer01\processor(_total)\% processor time :
                          29.7896844697383

6/19/2019 15:35:05        \\Computer01\processor(_total)\% processor time :
                          29.4962645638135

6/19/2019 15:35:06        \\Computer01\processor(_total)\% processor time :
                          25.5901500127408

Get-Counter 會使用 Counter 參數來指定 \Processor\% Processor Time 計數器。 Continuous 參數指定每秒取得一次樣本,直到用 CTRL+C命令停止為止。

範例 4:計數器集合的字母清單

這個範例會使用管線來取得計數器清單集,然後依字母順序排序列表。

Get-Counter -ListSet * |
  Sort-Object -Property CounterSetName |
    Format-Table CounterSetName, CounterSetType -AutoSize

CounterSetName                        CounterSetType
--------------                        --------------
.NET CLR Data                         SingleInstance
.NET Data Provider for SqlServer      SingleInstance
AppV Client Streamed Data Percentage  SingleInstance
Authorization Manager Applications    SingleInstance
BitLocker                             MultiInstance
Bluetooth Device                      SingleInstance
Cache                                 SingleInstance
Client Side Caching                   SingleInstance

Get-Counter 使用 ListSet 參數搭配星號 (*) 來取得計數器集合的完整清單。 CounterSet 物件將沿著管線傳送下去。 Sort-Object 會使用 Property 參數來指定物件的排序方式是 CounterSetName。 物件會從管線向下傳送至 Format-TableAutoSize 參數會調整列寬度,以將列的截斷降至最低。

. 資料行中的點 () 代表本機電腦。

範例 5:執行背景工作以取得計數器數據

在此範例中,Start-Job 在本機計算機上執行 Get-Counter 命令作為背景工作。 若要檢視作業的性能計數器輸出,請使用 Receive-Job Cmdlet。

Start-Job -ScriptBlock {
    Get-Counter -Counter "\LogicalDisk(_Total)\% Free Space" -MaxSamples 1000
}

Id     Name  PSJobTypeName   State    HasMoreData  Location   Command
--     ----  -------------   -----    -----------  --------   -------
1      Job1  BackgroundJob   Running  True         localhost  Get-Counter -Counter

Start-Job 會使用 ScriptBlock 參數來執行 Get-Counter 命令。 Get-Counter 會使用 Counter 參數來指定計數器路徑 \LogicalDisk(_Total)\% Free SpaceMaxSamples 參數會指定取得計數器的 1000 個樣本。

範例 6:從多部計算機取得計數器數據

此範例會使用變數從兩部計算機取得性能計數器數據。

$DiskReads = "\LogicalDisk(C:)\Disk Reads/sec"
$DiskReads | Get-Counter -ComputerName Server01, Server02 -MaxSamples 10

Timestamp                 CounterSamples
---------                 --------------
6/21/2019 10:51:04        \\Server01\logicaldisk(c:)\disk reads/sec :
                          0

                          \\Server02\logicaldisk(c:)\disk reads/sec :
                          0.983050344269146

$DiskReads 變數會儲存 \LogicalDisk(C:)\Disk Reads/sec 計數器路徑。 $DiskReads 變數會從管線向下傳送至 Get-Counter計數器 是第一個位置參數,並接受儲存在 $DiskReads的路徑。 ComputerName 指定兩部計算機,maxSamples 指定從每部電腦取得 10 個樣本。

範例 7:從多部隨機計算機取得計數器的實例值

此範例會取得企業中 50 部隨機遠端電腦上的性能計數器值。 ComputerName 參數會使用儲存在變數中的隨機電腦名稱。 若要更新變數中的電腦名稱,請重新建立 變數。

ComputerName 參數中伺服器名稱的替代方法是使用文字檔。 例如:

-ComputerName (Get-Random (Get-Content -Path C:\Servers.txt) -Count 50)

計數器路徑包含實例名稱中的星號 (*),以取得每個遠端電腦處理器的數據。

$Servers = Get-Random (Get-Content -Path C:\Servers.txt) -Count 50
$Counter = "\Processor(*)\% Processor Time"
Get-Counter -Counter $Counter -ComputerName $Servers

Timestamp                 CounterSamples
---------                 --------------
6/20/2019 12:20:35        \\Server01\processor(0)\% processor time :
                          6.52610319637854

                          \\Server01\processor(1)\% processor time :
                          3.41030663625782

                          \\Server01\processor(2)\% processor time :
                          9.64189975649925

                          \\Server01\processor(3)\% processor time :
                          1.85240835619747

                          \\Server01\processor(_total)\% processor time :
                          5.35768447160776

Get-Random Cmdlet 會使用 Get-Content,從 Servers.txt 檔案中選取 50 個隨機電腦名稱。 遠端電腦名稱會儲存在 $Servers 變數中。 \Processor(*)\% Processor Time 計數器的路徑會儲存在 $Counter 變數中。 Get-Counter 會使用 Counter 參數來指定 $Counter 變數中的計數器。 ComputerName 參數會指定 $Servers 變數中的電腦名稱。

範例 8:使用 Path 屬性來取得格式化的路徑名稱

這個範例會使用計數器集的 Path 屬性來尋找性能計數器的格式化路徑名稱。

管線會與 Where-Object Cmdlet 搭配使用,以尋找路徑名稱的子集。 若要找到計數器集合中完整的計數器路徑清單,請移除管線(|)和 Where-Object 指令。

$_ 是管線中目前對象的自動變數。 如需詳細資訊,請參閱 about_Automatic_Variables

(Get-Counter -ListSet Memory).Paths | Where-Object { $_ -like "*Cache*" }

\Memory\Cache Faults/sec
\Memory\Cache Bytes
\Memory\Cache Bytes Peak
\Memory\System Cache Resident Bytes
\Memory\Standby Cache Reserve Bytes
\Memory\Standby Cache Normal Priority Bytes
\Memory\Standby Cache Core Bytes
\Memory\Long-Term Average Standby Cache Lifetime (s)

Get-Counter 會使用 ListSet 參數來指定 Memory 計數器集。 命令會以括弧括住,讓 Paths 屬性以字串傳回每個路徑。 物件會從管線向下傳送至 Where-ObjectWhere-Object 會使用變數 $_ 來處理每一個物件,並使用 -like 運算符來尋找與字串 *Cache*相符的項目。 星號 (*) 是任何字元的通配符。

範例 9:使用 PathsWithInstances 屬性來取得格式化的路徑名稱

這個範例會取得格式化的路徑名稱,其中包含 PhysicalDisk 性能計數器的實例。

(Get-Counter -ListSet PhysicalDisk).PathsWithInstances

\PhysicalDisk(0 C:)\Current Disk Queue Length
\PhysicalDisk(_Total)\Current Disk Queue Length
\PhysicalDisk(0 C:)\% Disk Time
\PhysicalDisk(_Total)\% Disk Time
\PhysicalDisk(0 C:)\Avg. Disk Queue Length
\PhysicalDisk(_Total)\Avg. Disk Queue Length
\PhysicalDisk(0 C:)\% Disk Read Time
\PhysicalDisk(_Total)\% Disk Read Time

Get-Counter 會使用 ListSet 參數來指定 PhysicalDisk 計數器集。 命令會以括弧括住,讓 PathsWithInstances 屬性以字串傳回每個路徑實例。

範例 10:取得計數器集合中每個計數器的單一值

在此範例中,會針對本機計算機 記憶體 計數器集中的每個性能計數器傳回單一值。

$MemCounters = (Get-Counter -ListSet Memory).Paths
Get-Counter -Counter $MemCounters

Timestamp                 CounterSamples
---------                 --------------
6/19/2019 12:05:00        \\Computer01\memory\page faults/sec :
                          868.772077545597

                          \\Computer01\memory\available bytes :
                          9031176192

                          \\Computer01\memory\committed bytes :
                          8242982912

                          \\Computer01\memory\commit limit :
                          19603333120

Get-Counter 會使用 ListSet 參數來指定 Memory 計數器集。 命令會以括弧括住,讓 Paths 屬性以字串傳回每個路徑。 路徑會儲存在 $MemCounters 變數中。 Get-Counter 使用 Counter 參數來指定 $MemCounters 變數中的計數器路徑。

範例 11:顯示物件的屬性值

PerformanceCounterSample 物件中的屬性值代表每個數據範例。 在此範例中,我們使用 CounterSamples 對象的屬性來檢查、選取、排序及分組數據。

$Counter = "\\Server01\Process(Idle)\% Processor Time"
$Data = Get-Counter $Counter
$Data.CounterSamples | Format-List -Property *

Path             : \\Server01\process(idle)\% processor time
InstanceName     : idle
CookedValue      : 198.467899571389
RawValue         : 14329160321003
SecondValue      : 128606459528326201
MultipleCount    : 1
CounterType      : Timer100Ns
Timestamp        : 6/19/2019 12:20:49
Timestamp100NSec : 128606207528320000
Status           : 0
DefaultScale     : 0
TimeBase         : 10000000

計數器路徑會儲存在 $Counter 變數中。 Get-Counter 取得計數器值的其中一個範例,並將結果儲存在 $Data 變數中。 $Data 變數會使用 CounterSamples 屬性來取得物件的屬性。 物件會從管線向下傳送至 Format-ListProperty 參數會使用星號 (*) 通配符來選取所有屬性。

範例 12:性能計數器陣列值

在此範例中,變數會儲存每個性能計數器。 CounterSamples 屬性是一個可以顯示特定計數器值的陣列。

若要顯示每個計數器範例,請使用 $Counter.CounterSamples

$Counter = Get-Counter -Counter "\Processor(*)\% Processor Time"
$Counter.CounterSamples[0]

Path                                         InstanceName        CookedValue
----                                         ------------        -----------
\\Computer01\processor(0)\% processor time   0              1.33997091699662

Get-Counter 使用 Counter 參數來指定計數器 \Processor(*)\% Processor Time。 這些值會儲存在 $Counter 變數中。 $Counter.CounterSamples[0] 顯示第一個計數器值的陣列值。

範例 13:比較性能計數器值

此範例會尋找本機電腦上每個處理器所使用的處理器時間量。 CounterSamples 屬性可用來比較計數器數據與指定的值。

若要顯示每個計數器範例,請使用 $Counter.CounterSamples

$Counter = Get-Counter -Counter "\Processor(*)\% Processor Time"
$Counter.CounterSamples | Where-Object { $_.CookedValue -lt "20" }

Path                                         InstanceName        CookedValue
----                                         ------------        -----------
\\Computer01\processor(0)\% processor time   0              12.6398025240208
\\Computer01\processor(1)\% processor time   1              15.7598095767344

Get-Counter 使用 Counter 參數來指定計數器 \Processor(*)\% Processor Time。 這些值會儲存在 $Counter 變數中。 儲存在 $Counter.CounterSamples 中的物件會送入管線。 Where-Object 使用文稿區塊來比較每個物件值與指定的值 20$_.CookedValue 是管線中目前物件的變數。 有 CookedValue 小於 20 的計數器會被顯示。

範例 14:排序性能計數器數據

此範例示範如何排序性能計數器數據。 此範例會在取樣期間尋找電腦上使用最多處理器時間的程序。

$Procs = Get-Counter -Counter "\Process(*)\% Processor Time"
$Procs.CounterSamples | Sort-Object -Property CookedValue -Descending |
   Format-Table -Property Path, InstanceName, CookedValue -AutoSize

Path                                                         InstanceName             CookedValue
----                                                         ------------             -----------
\\Computer01\process(_total)\% processor time                _total              395.464129650573
\\Computer01\process(idle)\% processor time                  idle                389.356575524695
\\Computer01\process(mssense)\% processor time               mssense             3.05377706293879
\\Computer01\process(csrss#1)\% processor time               csrss               1.52688853146939
\\Computer01\process(microsoftedgecp#10)\% processor time    microsoftedgecp     1.52688853146939
\\Computer01\process(runtimebroker#5)\% processor time       runtimebroker                      0
\\Computer01\process(settingsynchost)\% processor time       settingsynchost                    0
\\Computer01\process(microsoftedgecp#16)\% processor time    microsoftedgecp                    0

Get-Counter 會使用 Counter 參數來指定本機電腦上所有處理程序的 \Process(*)\% Processor Time 計數器。 結果會儲存在 $Procs 變數中。 具有 $Procs 屬性的 變數會在管線下傳送 PerformanceCounterSample 物件。 Sort-Object 會使用 Property 參數,以 CookedValue 作為基準,按照 遞減 順序來排序物件。 Format-Table 使用 Property 參數來選取輸出的數據行。 AutoSize 參數會調整列寬度,以將列的截斷降至最低。

參數

-ComputerName

指定一個電腦名稱或 遠端 電腦名稱的逗號分隔數位。 使用 NetBIOS 名稱、IP 位址或電腦的完整網域名稱。

若要從 本機 計算機取得性能計數器數據,請排除 ComputerName 參數。 對於包含 MachineName 資料行的 ListSet 等輸出,點(.)表示本機電腦。

Get-Counter 不依賴 PowerShell 遠端處理。 即使您的電腦未設定為執行遠端命令,您也可以使用 ComputerName 參數。

類型:String[]
別名:Cn
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Continuous

指定 連續 時,Get-Counter 不斷取得樣本,直到 您按下 CTRL+C為止。 每個指定的性能計數器每秒都會取得樣本。 使用 SampleInterval 參數來增加連續樣本之間的間隔。

類型:SwitchParameter
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Counter

指定一或多個計數器的路徑。 路徑可以作為逗號分隔的陣列、變數,或文字檔中的值來輸入。 您可以將計數器路徑字串傳送到管線 Get-Counter

計數器路徑使用下列語法:

\\ComputerName\CounterSet(Instance)\CounterName

\CounterSet(Instance)\CounterName

例如:

\\Server01\Processor(*)\% User Time

\Processor(*)\% User Time

\\ComputerName 在性能計數器路徑中是選擇性的。 如果計數器路徑不包含計算機名稱,Get-Counter 會使用本機計算機。

實例中的星號 (*) 是取得計數器所有實例的通配符。

類型:String[]
Position:0
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:True

-ListSet

列出電腦上的性能計數器集合。 使用星號 (*) 來指定所有計數器集合。 輸入一個名稱或以逗號分隔的計數器集合名稱字串。 您可以透過管線傳送計數器集名稱。

若要取得格式化計數器路徑的計數器集合,請使用 ListSet 參數。 路徑路徑與實例 屬性在每個計數器集中都包含格式化為字串的個別計數器路徑。

您可以將計數器路徑字串儲存在變數中,或使用管線將字串傳送至另一個 Get-Counter 命令。

例如,要將每個 處理器的 計數器路徑發送至 Get-Counter

Get-Counter -ListSet Processor | Get-Counter

注意

在 PowerShell 7 中,Get-Counter 無法擷取計數器集的 Description 屬性。 Description 會設定為 $null

類型:String[]
Position:0
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:True

-MaxSamples

指定要從每個指定的性能計數器取得的樣本數目。 若要取得樣本的常數數據流,請使用 Continuous 參數。

如果未指定 MaxSamples 參數,Get-Counter 只會為每個指定的計數器取得一個樣本。

若要收集大型數據集,請以PowerShell背景作業的形式執行 Get-Counter。 如需詳細資訊,請參閱 about_Jobs

類型:Int64
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-SampleInterval

指定每個指定性能計數器樣本之間的秒數。 如果未指定 SampleInterval 參數,Get-Counter 會使用一秒的間隔。

類型:Int32
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

輸入

String[]

Get-Counter 接受計數器路徑和計數器集名稱的管線輸入。

輸出

CounterSet

使用 ListSet 參數,此 Cmdlet 會傳回 CounterSet 物件。

PerformanceCounterSampleSet

根據預設,使用 Counter 參數,此 Cmdlet 會傳回 PerformanceCounterSampleSet 物件。

備註

如果未指定任何參數,Get-Counter 會針對每個指定的性能計數器取得一個範例。 使用 MaxSamplesContinuous 參數來取得更多範例。

Get-Counter 會使用樣本之間的一秒間隔。 使用 SampleInterval 參數來增加間隔。

MaxSamplesSampleInterval 值會套用至命令中每部電腦上的所有計數器。 若要為不同的計數器設定不同的值,請輸入不同的 Get-Counter 命令。

在 PowerShell 7 中使用 ListSet 參數時,Get-Counter 無法擷取計數器集合的 Description 属性。 Description 會設定為 $null