PowerShellでパフォーマンス カウンタ オブジェクト(System.Diagnostics.PerformanceCounter)を使用して、パフォーマンス カウンタの値を取得します。
下記のコードではプロセッサの利用状況を1秒おきに取得しています。
$p = new-object System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total")
[Void] $p.NextValue()
while($true){
Start-Sleep -s 1
Write-Output ([string]::Format("{0:n2} %", $p.NextValue()))
}
パフォーマンスカウンタなのでネットワークの使用状況や
$n = new-object System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Total/sec", "インスタンス名")
[Void] $n.NextValue()
while($true){
Start-Sleep -s 1
Write-Output ([string]::Format("{0:n0} Bytes/Sec", $n.NextValue()))
}
メモリの空きなども取得できます。
$m = new-object System.Diagnostics.PerformanceCounter("Memory", "Available MBytes", "")
[Void] $m.NextValue()
Write-Output ([string]::Format("{0} MB", $m.NextValue()))