2016-07-29 58 views
3

我有這樣的腳本:降低輸出到一條線

$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

回答

4

一次收集所有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-SampleIntervalGet-Counter。循環不是必需的。

$interval = 5 # seconds 

Get-Counter -Counter $ws, $wsPeak, $wsPriv -Continuous -SampleInterval $interval | 
    Select-Object ...