从本地和远程计算机上获取性能计数器数据。

语法

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 名称、Internet 协议 (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”合并值。它每两秒钟收集一次数据,直到具有三个值。

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

说明
-----------
这些命令获取本地计算机上的 memory 计数器集中的所有性能计数器的单个值。

第一个命令获取计数器路径并将它们保存在 $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






另请参阅




目录