powershell - 将三个命令的输出减少到一行

标签 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 $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/

相关文章:

mysql - PowerShell 不会逐行迭代 MySQL 结果 - 为什么?

java - 从行中分割 ("[\t ]") 是什么意思

CruiseControl.net 中的 Powershell 在部署新版本代码之前备份现有文件夹

powershell - 如何将 Invoke-SilentlyAndReturnExitCode 实现为 Powershell 模块功能?

php - PHP推理变量输出原理

node.js - Node - GraphQL - Ad.user 字段类型必须是输出类型,但得到 : undefined

javascript - d3.js zoom.translate 向量的单位是什么?

Powershell 文件输出大小的两倍

c++ - 术语信号事件中的 Windows 进程退出代码

c++ - Doxygen @param 方向参数 [in],[out],[in,out] 示例输出