從本機和遠端電腦取得效能計數器資料。

語法

Get-Counter [-Counter] <string[]> [-ComputerName <string[]>] [-Continuous] [-MaxSamples <Int64>] [-SampleInterval <int>] [<CommonParameters>]

Get-Counter -ListSet <string[]> [-ComputerName <string[]>] [<CommonParameters>]

描述

Get-Counter cmdlet 會從 Windows 的效能監控檢測中,直接取得即時的效能計數器資料。您可以使用它從本機或遠端電腦,取得您所指定取樣間隔的效能資料。

如果沒有指定參數,則 "Get-Counter" 命令會取得系統計數器的計數器資料集。

您可以使用 Get-Counter 的參數指定一個或多個電腦、列出效能計數器組及其包含的計數器,並設定樣本大小和間隔。

參數

-ComputerName <string[]>

從指定的電腦取得資料。請輸入電腦的 NetBIOS 名稱、網際網路通訊協定 (IP) 位址或完整網域名稱。預設值為本機電腦。

注意:Get-Counter 並不依存於 Windows PowerShell 遠端。即使沒有在 Windows PowerShell 中將電腦設定成遠端,也可以使用 Get-Counter 事件的 ComputerName 參數。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Continuous

連續進行取樣,直到您按下 CTRL+C 為止。根據預設,Get-Counter 只會取得一個計數器樣本。您可以利用 SampleInterval 參數設定連續取樣的間隔。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Counter <string[]>

從指定的效能計數器取得資料。輸入一個或多個計數器路徑。只有在 Instance 值中才允許使用萬用字元。您也可以經由管道將計數器路徑字串輸出至 Get-Counter。

每個計數器路徑具有下列格式:

"[\\<ComputerName>]\<CounterSet>(<Instance>)\<CounterName>"

例如:

"\\Server01\Processor(2)\% User Time"。

<ComputerName> 為選擇性項目。如果省略該項目,Get-Counter 會使用 ComputerName 參數的值。

注意:為了取得格式正確的計數器路徑,請使用 ListSet 參數取得效能計數器組。每個效能計數器組的 Paths 和 PathsWithInstances 屬性,都包含以字串為格式的個別計數器路徑。您可以將計數器路徑字串儲存在變數中,或經由管道將字串直接輸出至另一個 Get-Counter 命令。如需示範,請參閱範例。

必要?

true

位置?

2

預設值

接受管線輸入?

true (ByValue)

接受萬用字元?

true

-ListSet <string[]>

取得電腦上的指定效能計數器組。請輸入計數器組的名稱。允許使用萬用字元。您也可以經由管道將計數器組名稱輸出至 Get-Counter。

必要?

true

位置?

named

預設值

接受管線輸入?

true (ByValue, ByPropertyName)

接受萬用字元?

true

-MaxSamples <Int64>

指定可以從每個計數器取得的樣本數量。預設值為 1 個樣本。若要進行連續取樣 (沒有樣本數量上限),請使用 Continuous 參數。

若要收集非常大的資料集,請考慮將 Get-Counter 命令以 Windows PowerShell 背景工作的方式執行。如需要詳細資訊,請參閱 about_Jobs 和 Start-Job。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-SampleInterval <int>

指定樣本間的秒數。最小值和預設值為 1 秒。

必要?

false

位置?

named

預設值

1

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

System.String[]

您可以經由管道將計數器路徑和計數器組 (ListSet) 名稱輸出至 Get-Counter。

輸出

Microsoft.PowerShell.Commands.GetCounter.CounterSet, Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet, Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample

ListSet 參數會取得 Microsoft.PowerShell.Commands.GetCounter.CounterSet 物件。Counter 參數會取得 Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet 物件。每個計數器值為一個 Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample 物件。

附註

效能計數器通常受到存取控制清單 (ACL) 的保護。若要取得所有可用的效能計數器,請利用 [以系統管理員身分執行] 選項開啟 Windows PowerShell。

根據預設,Get-Counter 在一秒的取樣間隔期間,只會取得一個範例。若要變更這個行為,請使用 MaxSamples 與 Continuous 參數。

您在命令中設定的 MaxSamples 和 SampleInterval 值,會套用到所有電腦上的所有計數器。若要針對不同計數器設定不同值,請針對每個計數器輸入個別的 Get-Counter 命令。

範例 1

C:\PS># Get-Counter

描述
-----------
這個命令會取得本機電腦上的所有計數器組。

C:\PS> get-counter -ListSet *

因為許多計數器組都受到存取控制清單 (ACL) 的保護,所以若要查看所有的計數器組,在使用 Get-Counter 命令前,請使用 [以系統管理員身分執行] 選項開啟 Windows PowerShell。






範例 2

C:\PS># Get-Counter

描述
-----------
這個命令會取得本機電腦上所有處理器目前的 "% Processor Time" 組合值。在具有 3 個值之前,該命令每 2 秒就會收集資料。

C:\PS> get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 3






範例 3

C:\PS># Get-Counter

描述
-----------
這個命令會取得本機電腦上的所有計數器組名稱清單,以字母順序排序。

C:\PS> get-counter -listset * | sort-object  countersetname | format-table countersetname






範例 4

C:\PS># Get-Counter

描述
-----------
這些命令會使用計數器組的 Path 屬性,找出格式正確的效能計數器路徑名稱。您可以使用像這樣的命令取得正確的計數器路徑名稱。

第一個命令會取得本機電腦上 Memory 計數器組中效能計數器的路徑名稱。

C:\PS> (get-counter -listset memory).paths

\Memory\Page Faults/sec
\Memory\Available Bytes
\Memory\Committed Bytes
\Memory\Commit Limit
\Memory\Write Copies/sec
\Memory\Transition Faults/sec
\Memory\Cache Faults/sec
\Memory\Demand Zero Faults/sec
\Memory\Pages/sec
\Memory\Pages Input/sec
...

第二個命令會取得包含 "cache" 的路徑名稱。

C:\PS> (get-counter -listset memory).paths | where {$_ -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






範例 5

C:\PS># Get-Counter

描述
-----------
這些命令會從 Server01 和 Server02 電腦取得 Disk Reads/sec 計數器資料。

第一個命令會將 Disk Reads/sec 計數器路徑儲存在 $diskreads 變數中。

C:\PS> $diskreads = "\LogicalDisk(C:)\Disk Reads/sec"

第二個命令會使用管線運算子 (|) 將 $diskreads 變數中的計數器路徑傳送給 Get-Counter Cmdlet,命令中使用 MaxSamples 參數,將輸出內容限制為 10 個樣本。

C:\PS> $diskreads | get-counter -computer Server01, Server02 -maxsamples 10






範例 6

C:\PS># Get-Counter

描述
-----------
這個命令會取得格式正確的 PhysicalDisk 效能計數器路徑名稱,包含執行個體名稱。

C:\PS> (get-counter -list physicaldisk).pathswithinstances






範例 7

C:\PS># Get-Counter

描述
-----------
這些命令會在企業中隨機選取的 50 台電腦上,取得 "% DPC Time" 效能計數器值。

第二個命令使用 Get-Content Cmdlet,從 Servers.txt 檔案取得企業伺服器清單。其中使用 Get-Random cmdlet,從 Servers.txt 檔案內容中隨機選取 50 個伺服器名稱。結果會儲存在 $servers 變數中。

C:\PS> $servers = get-random (get-content servers.txt) -count 50

第二個命令會將 "% DPC Time" Cmdlet 計數器路徑儲存在 $Counter 變數中。計數器路徑在執行個體名稱中包含萬用字元,以取得每台電腦上所有處理器的資料。

C:\PS> $counter = "\Processor(*)\% DPC Time"

第三個命令使用 Get-Counter Cmdlet 取得計數器值。命令使用 Counter 參數指定計數器,並且使用 ComputerName 參數指定儲存於 $servers 變數中的電腦。

C:\PS> get-counter -Counter $counter -computername $servers






範例 8

C:\PS># Get-Counter

描述
-----------
這些命令會取得本機電腦上記憶體計數器組中所有效能計數器的單一值。

第一個命令會取得計數器路徑並儲存在 $memCounters 變數中。

C:\PS> $memCounters = (get-counter -list memory).paths

第二個命令使用 Get-Counter cmdlet,取得每個計數器的計數器資料。並使用 Counter 參數指定 $memCounters 中的計數器。

C:\PS> get-counter -counter $memCounters






範例 9

C:\PS># Get-Counter

描述
-----------
這個範例示範用於表示每個資料樣本的 PerformanceCounterSample 物件的屬性值。

第一個命令會在 $counter 變數中儲存計數器路徑。

C:\PS> $counter = "\\SERVER01\Process(Idle)\% Processor Time"

第二個命令使用 Get-Counter cmdlet,取得計數器值的一個樣本。並將結果儲存在 $data 變數中。

C:\PS> $data = get-counter $counter

第三個命令使用 Format-List Cmdlet,將樣本集物件的所有 CounterSamples 屬性列為清單顯示。

C:\PS> $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        : 7/15/2008 6:39:12 PM
Timestamp100NSec : 128606207528320000
Status           : 0
DefaultScale     : 0
TimeBase         : 10000000

您可以使用 CounterSamples 物件的屬性,進行資料檢查、選取、排序和群組化。






範例 10

C:\PS># Get-Counter

描述
-----------
這個命令會將 Get-Counter 命令以背景工作的方式執行。如需詳細資訊,請參閱 Start-Job。

C:\PS> $counters = "\LogicalDisk(_Total)\% Free Space"

C:\PS> start-job -scriptblock {get-counter -counter $counters -maxsamples 1000)






範例 11

C:\PS># Get-Counter

描述
-----------
這個命令會使用 Get-Counter 和 Get-Random cmdlet,從 Servers.txt 檔案中隨機選取的 50 台電腦上,找出可用磁碟空間百分比。

C:\PS> get-counter -computername (get-random servers.txt -count 50) -counter "\LogicalDisk(*)\% Free Space"






範例 12

C:\PS># Get-Counter

描述
-----------
這個範例示範如何讓計數器資料與其來源電腦產生關聯,以及如何操作資料。


第一個命令使用 Get-Counter cmdlet,從兩台遠端電腦 S1 和 S2 取得 "LogicalDisk\% Free Space" 計數器值。並將結果儲存在 $a 變數中。

$a = get-counter "\LogicalDisk(_Total)\% Free Space" -comp s1, s2



第二個命令會顯示 $a 變數中的結果。所有的資料都儲存在該物件中,但在這種格式下並不容易了解。

C:\PS> $a

Counter Paths: \\s1\\logicaldisk(c:)\% free space, \\s1\\logicaldisk(d:)\% free space, \\s1\\logicaldisk(_total)\% free space, \\s2\\logicaldisk(c:)\% free space, \\s2\\logicaldisk(_total)\% free space

Timestamp     : 7/15/2008 5:09:08 PM
Cooked Values : "0.327058823529412", "17.8952248493278", "12.9994033060778", "75.0754805595626", "75.0754805595626"



第三個命令會以表格的方式,顯示 Get-Counter 所傳回 PerformanceCounterSampleSet 物件的 CounterSamples 屬性值 (若要查看該物件的所有屬性和方法,請經由管線輸出至 Get-Member cmdlet)。

C:\PS> $a.countersamples | format-table -auto

Path                                     InstanceName       CookedValue
----                                     ------------       -----------
\\s1\\logicaldisk(c:)\% free space       c:           0.327058823529412
\\s1\\logicaldisk(d:)\% free space       d:            17.8952248493278
\\s1\\logicaldisk(_total)\% free space   _total        12.9994033060778
\\s2\\logicaldisk(c:)\% free space       c:            75.0754805595626
\\s2\\logicaldisk(_total)\% free space   _total        75.0754805595626

CounterSamples 屬性包含 PerformanceCounterSample 物件,以及其自己的屬性和方法。第四個命令使用陣列標記法取得第一個計數器樣本,並使用管線運算子將計數器樣本物件傳送給 Format-List cmdlet,後者會以清單顯示其所有屬性和方法。這樣可以顯示每個計數器樣本物件中的資料豐富性。



第四個命令顯示如何選取計數器樣本中的資料。其中使用 Where-Object cmdlet,只取得 CookedValue 小於 15 的計數器樣本。

C:\PS> $a.countersamples | where {$_.cookedvalue -lt 15}

Path                                     InstanceName       CookedValue
----                                     ------------       -----------
\\s1\\logicaldisk(c:)\% free space       c:           0.327058823529412
\\s1\\logicaldisk(_total)\% free space   _total        12.9994033060778






範例 13

C:\PS># Get-Counter

描述
-----------
這個範例示範如何排序您擷取到的效能計數器資料。範例會找出取樣期間內,電腦上使用最多處理器時間的處理程序。

第一個命令會取得電腦上所有處理程序的 "Process\% Processor Time" 計數器。此命令會將結果儲存在 $p 變數中。

C:\PS> $p = get-counter '\Process(*)\% Processor Time'


第二個命令會取得 $p 中樣本集物件的 CounterSamples 屬性,並依據樣本處理後的值,以遞減順序排序樣本。命令中使用 Format-Table cmdlet 及其 AutoFormat 參數,定位表格中的欄位。

C:\PS> $p.CounterSamples | sort-object -property CookedValue -Descending | format-table -auto

Path                                              InstanceName      CookedValue
----                                              ------------      -----------
\\server01\process(_total)\% processor time        _total        200.00641042078
\\server01\process(idle)\% processor time          idle          200.00641042078
\\server01\process(explorer#1)\% processor time    explorer                    0
\\server01\process(dwm#1)\% processor time         dwm                         0
\\server01\process(taskeng#1)\% processor time     taskeng                     0
\\server01\process(taskhost#1)\% processor time    taskhost                    0
\\server01\process(winlogon)\% processor time      winlogon                    0
\\server01\process(csrss)\% processor time         csrss                       0






範例 14

C:\PS># Get-Counter

描述
-----------
這些命令會找出電腦上具有最大工作集的處理程序。它會依據其工作集大小,以遞減順序列出處理程序。

第一個命令會針對每個處理程序,取得 "Process\Working Set - Private" 計數器的一個樣本。該命令會在 $ws 變數中儲存計數器資料。

C:\PS> $ws = get-counter "\Process(*)\Working Set - Private"

第二個命令使用管線運算子 (|),將 $ws 變數 CounterSamples 屬性中的資料傳送給 Sort-Object cmdlet,後者會依據 CookedValue 屬性值,以遞減順序排序處理程序資料。另一個管線會將排序好的資料傳送給 Format-Table cmdlet,後者會將資料格式化為具有 InstanceName 和 CookedValue 欄位的表格。

C:\PS> $ws.countersamples | sort-object -property cookedvalue -descending | format-table -property InstanceName, CookedValue -auto

InstanceName  CookedValue
------------  -----------
_total          162983936
svchost          40370176
powershell       15110144
explorer         14135296
svchost          10928128
svchost           9027584
...






範例 15

C:\PS># Get-Counter

描述
-----------
這個命令會在 1 秒間隔的期間,取得 Processor\% Processor Time 計數器的一系列樣本。若要停止命令,請按 CTRL+C。

C:\PS> get-counter -counter "\processor(_total)\% processor time" -continuous






請參閱




目錄