gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:00:56 25 4
gpt4 key购买 nike

'晚间,

我是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

    例如“\ 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]))."

    任何帮助将不胜感激。

    最佳答案

    我戳了一下,结果掉了下来:

    $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
    }
    }

    关于powershell - PowerShell:为每个计数器计算rawvalue属性的连续值之间的增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711736/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com