我有这个脚本:
$counterWS = "\Process(powershell)\Working Set"
$counterWSPe = "\Process(powershell)\Working Set Peak"
$counterWSPr = "\Process(powershell)\Working Set - Private"
$dataWS = Get-Counter -Counter $counterWS
$dataWSPe = Get-Counter -Counter $counterWSPe
$dataWSPr = Get-Counter -Counter $counterWSPr
$dataWS.countersamples | Format-Table Timestamp,@{Name='WorkingSet';Expression={($_.CookedValue/1KB)}},WorkingSetPeak,WorkingSetPrivate -Auto | findstr Timestamp
$dataWS.countersamples | Format-Table Timestamp,@{Name='WorkingSet';Expression={($_.CookedValue/1KB)}},WorkingSetPeak,WorkingSetPrivate -Auto | findstr [-]
while ($true) {
$dataWS.countersamples | Format-Table Timestamp,@{Name='WorkingSet';Expression={($_.CookedValue/1KB)}},WorkingSetPeak,WorkingSetPrivate -Auto | findstr [:]
$dataWSPe.countersamples | Format-Table Timestamp,WorkingSet,@{Name='WorkingSetPeak';Expression={($_.CookedValue/1KB)}},WorkingSetPrivate -Auto | findstr [:]
$dataWSPr.countersamples | Format-Table Timestamp,WorkingSet,WorkingSetPeak,@{Name='WorkingSetPrivate';Expression={($_.CookedValue/1KB)}} -Auto | findstr [:]
Start-Sleep -s $args[0]
}
结果是这样的:
Timestamp WorkingSet WorkingSetPeak WorkingSetPrivate --------- ---------- -------------- ----------------- 29/07/2016 18:41:12 10644 29/07/2016 18:41:13 10676 29/07/2016 18:41:14 3056
有没有办法减少这样的输出:
Timestamp WorkingSet WorkingSetPeak WorkingSetPrivate --------- ---------- -------------- ----------------- 29/07/2016 18:41:12 10644 10676 3056
最佳答案
一次收集所有 3 个计数器(参数 -Counter
接受参数列表),然后将结果拆分为单独的 calculated properties :
$ws = '\Process(powershell)\Working Set'
$wsPeak = '\Process(powershell)\Working Set Peak'
$wsPriv = '\Process(powershell)\Working Set - Private'
Get-Counter -Counter $ws, $wsPeak, $wsPriv |
Select-Object Timestamp,
@{n='WorkingSet';e={$_.CounterSamples[0].CookedValue / 1KB}},
@{n='WorkingSetPeak';e={$_.CounterSamples[1].CookedValue / 1KB}},
@{n='WorkingSetPrivate';e={$_.CounterSamples[2].CookedValue / 1KB}}
CounterSamples
属性是 PerformanceCounterSample
的列表可以通过索引单独访问的对象。
如果您不想依赖按传递给 Get-Counter
的参数顺序返回的结果,您可以按路径选择它们,例如像这样:
@{n='WorkingSetPeak';e={
($_.CounterSamples | Where-Object { $_.Path -like '*peak' }).CookedValue / 1KB
}}
对于连续样本收集,将参数-Continuous
和-SampleInterval
添加到Get-Counter
。不需要循环。
$interval = 5 # seconds
Get-Counter -Counter $ws, $wsPeak, $wsPriv -Continuous -SampleInterval $interval |
Select-Object ...
关于powershell - 将三个命令的输出减少到一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667973/