Powershell 将输出从三个命令减少到一行
我有这个剧本:Powershell 将输出从三个命令减少到一行,powershell,output,line,Powershell,Output,Line,我有这个剧本: $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 $c
$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
时间戳工作集工作集峰值工作集专用
--------- ---------- -------------- -----------------
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
@{n='WorkingSetPeak';e={
($_.CounterSamples | Where-Object { $_.Path -like '*peak' }).CookedValue / 1KB
}}
时间戳工作集工作集峰值工作集专用
--------- ---------- -------------- -----------------
2016年7月29日18:41:12 10644 10676 3056一次收集所有3个计数器(参数
-Counter
接受参数列表),然后将结果拆分为单独的:
CounterSamples
属性是可以通过索引单独访问的对象列表
如果您不想依赖按照传递给Get Counter
的参数顺序返回的结果,您可以通过它们的路径选择它们,例如如下所示:
Timestamp WorkingSet WorkingSetPeak WorkingSetPrivate
--------- ---------- -------------- -----------------
29/07/2016 18:41:12 10644 10676 3056
@{n='WorkingSetPeak';e={
($_.CounterSamples | Where-Object { $_.Path -like '*peak' }).CookedValue / 1KB
}}
对于连续样本采集,将参数-continuous
和-SampleInterval
添加到获取计数器
。不需要循环
$interval = 5 # seconds
Get-Counter -Counter $ws, $wsPeak, $wsPriv -Continuous -SampleInterval $interval |
Select-Object ...