PowerShell:计算每个计数器的rawvalue属性的连续值之间的增量

时间:2015-04-18 01:09:43

标签: powershell powershell-v2.0

“晚上,

我是PowerShell脚本(以及此站点)的新手,需要编写一个脚本:

  • 多次收集性能计数器(在本例中为TCP)
  • 计算每个计数器的rawvalue属性的连续值之间的差异

规定每个间隔T脚本遍历性能计数器N次,并且所述性能计数器为“A”和“B”,并且我们从0开始计算,它需要执行以下计算:

A[1st] - A[0th], 
A[2nd] - A[1st], 
A[3rd] - A[2nd]
...  

目前,脚本只迭代计数器两次(即在这种情况下N = 2)。目标是能够“多次”(例如几百次)迭代这些计数器。

目前,该脚本将每个计数器的原始值读入一个数组。这是:

$cntr = (get-counter -listset tcpv4).paths
$arry = @()

for ($i=0; $i -lt 2; $i++) {
    write-host "`nThis is iteration $i`n"
    foreach ($elmt in $cntr) {
        $z = (get-counter -counter $elmt).countersamples[0].rawvalue
        $arry = $arry + $z
        write-host "$elmt is: $z`n"
    }
}

当我运行此脚本时,我得到的输出如下:

This is iteration 0

\TCPv4\Segments/sec is: 24723

\TCPv4\Connections Established is: 27

\TCPv4\Connections Active is: 796

\TCPv4\Connections Passive is: 47

\TCPv4\Connection Failures is: 158

\TCPv4\Connections Reset is: 412

\TCPv4\Segments Received/sec is: 14902

\TCPv4\Segments Sent/sec is: 9822

\TCPv4\Segments Retransmitted/sec is: 199


This is iteration 1

\TCPv4\Segments/sec is: 24727

\TCPv4\Connections Established is: 27

\TCPv4\Connections Active is: 798

\TCPv4\Connections Passive is: 47

\TCPv4\Connection Failures is: 159

\TCPv4\Connections Reset is: 412

\TCPv4\Segments Received/sec is: 14903

\TCPv4\Segments Sent/sec is: 9824

\TCPv4\Segments Retransmitted/sec is: 200

e.g。 “\ TCPv4 \ Segments Retransmitted / sec”计数器的rawvalue属性的两个值分别是$ arry [8]和$ arry [17]。为了得出我正在使用的两者之间的差异:

write-host "The difference between the successive counters for $($cntr[-1]) is $($($arry[17]) - $($arry[8]))."

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我戳了一下,然后就掉了下来:

$cntr = (get-counter -listset tcpv4).paths
$LastValue = @{}

Get-Counter $cntr -SampleInterval 2 -MaxSamples 5 |
foreach {
  foreach ($Sample in $_.CounterSamples)
   {
     $ht = [Ordered]@{
        Counter   = $Sample.path.split('\')[-1]
        TimeStamp = $_.TimeStamp
        RawValue  = $Sample.RawValue
        LastValue = $LastValue[$Sample.Path]
        Change    = $Sample.RawValue - $LastValue[$Sample.Path]
        }
     if ($LastValue.ContainsKey($Sample.path))
       { [PSCustomObject]$ht }
     $LastValue[$Sample.Path] = $Sample.RawValue
   }
 } 

编辑:

这应该适用于V2:

$cntr = (get-counter -listset tcpv4).paths
$LastValue = @{}

Get-Counter $cntr -SampleInterval 10 -MaxSamples 3 |
foreach {
  foreach ($Sample in $_.CounterSamples)
   {
     $Object = '' | 
      Select-Object Counter,TimeStamp,RawValue,LastValue,Change
      $Object.Counter   = $Sample.path.split('\')[-1]
      $Object.TimeStamp = $_.TimeStamp
      $Object.RawValue  = $Sample.RawValue
      $Object.LastValue = $LastValue[$Sample.Path]
      $Object.Change    = $Sample.RawValue - $LastValue[$Sample.Path]

     if ($LastValue.ContainsKey($Sample.path))
       { $Object }

     $LastValue[$Sample.Path] = $Sample.RawValue
   }
 } 

答案 1 :(得分:0)

确定。那么让我们使用它

$cntr = (get-counter -listset tcpv4).paths
$arry = @()
$maximumIterations = 2  # Variable based since you intended to change this. 

# Cycle the counters while recording the values. Once completed we will calculate change.
for ($i=1; $i -le $maximumIterations; $i++) {
    foreach ($elmt in $cntr) {
        $arry += New-Object -TypeName PsCustomObject -Property @{
            Iteration = $i
            Counter = $elmt
            RawValue = (get-counter -counter $elmt).countersamples[0].rawvalue
            Change = $null
        }
    }
}

# Now that we have all the values lets calculate the rate of change over each iteration.
$arry | Where-Object{$_.Iteration -gt 1} | ForEach-Object{
    $previousIteration = $_.Iteration - 1 
    $thisCounter = $_.Counter
    $thisValue = $_.RawValue
    $previousValue = ($arry | Where-Object{$_.Counter -eq $thisCounter -and $_.Iteration -eq $previousIteration}).RawValue
    $_.Change = $thisValue - $previousValue
}

$arry | Select Iteration,Counter,RawValue,Change

并非我们拥有,但我收集了所有计数器数据及其迭代值,就像您对Write-Host所做的那样。您会注意到我为Change创建了一个占位符,但是没有填充它。在计算之前$arry将包含如下数据。注意:输出被截断

Iteration Change Counter                           RawValue
--------- ------ -------                           --------
        1        \TCPv4\Segments/sec               28324837
        1        \TCPv4\Connections Established         120
        .        ..............................       .....
        2        \TCPv4\Segments/sec               28325441
        2        \TCPv4\Connections Established         125

一旦将所有数据收集到$arry中,我们就会获得不是第一个的所有迭代,并单独处理每个项目。使用管道中当前项的数据,我们将其与先前的迭代值进行匹配。使用与上面相同的值,我们得到您希望监控的更改

Iteration Counter                           RawValue Change
--------- -------                           -------- ------
        1 \TCPv4\Segments/sec               28324837       
        1 \TCPv4\Connections Established         120 
        . ..............................       .....         
        2 \TCPv4\Segments/sec               28325441 604   
        2 \TCPv4\Connections Established         125 5