Get-Counter

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

Syntax

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

Description

此 Cmdlet 僅適用於 Windows 平臺。

Cmdlet 會 Get-Counter 直接從 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會使用 ListSet 參數搭配星號 (*) 來取得計數器集合的清單。 MachineName 資料行中的點 (.) 代表本機電腦。

範例 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 Time。 SampleInterval 參數會設定兩秒的間隔來檢查計數器。 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 參數會調整數據行寬度,以將截斷降至最低。

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

範例 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

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

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

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

管線會與 Cmdlet 搭配 Where-Object 使用,以尋找路徑名稱的子集。 若要尋找計數器集合計數器路徑的完整清單,請移除管線 (|) 和 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 參數來指定記憶體計數器集合。 命令會以括弧括住,讓 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 參數來指定記憶體計數器集合。 命令會以括弧括住,讓 Paths 屬性以字串的形式傳回每個路徑。 路徑會儲存在變數中 $MemCountersGet-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

計數器路徑會儲存在變數中 $CounterGet-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是管線中目前物件的變數。 顯示 具有小於 20 的 CookedValue 計數器。

範例 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具有 CounterSamples 屬性的變數會將 PerformanceCounterSample 物件傳送至管線。 Sort-Object會使用 Property 參數,以遞減順序依 CookedValue 排序物件。 Format-Table會使用 Property 參數來選取輸出的數據行。 AutoSize 參數會調整數據行寬度,以將截斷降至最低。

參數

-ComputerName

指定一個電腦名稱或遠端電腦名稱的逗號分隔陣列。 使用 NetBIOS 名稱、IP 位址或電腦的完整功能變數名稱。

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

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

Type:String[]
Aliases:Cn
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Continuous

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

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Counter

指定一或多個計數器路徑的路徑。 路徑是輸入做為逗號分隔數位、變數或文字檔中的值。 您可以將管線中的計數器路徑字串傳送至 Get-Counter

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

\\ComputerName\CounterSet(Instance)\CounterName

\CounterSet(Instance)\CounterName

例如:

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

\Processor(*)\% User Time

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

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

Type:String[]
Position:0
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:True

-ListSet

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

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

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

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

Get-Counter -ListSet Processor | Get-Counter

注意

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

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-MaxSamples

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

如果未指定 MaxSamples 參數,Get-Counter則每個指定的計數器只會取得一個範例。

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

Type:Int64
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-SampleInterval

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

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters: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