'vening,
我是PowerShell脚本的新手(也是这个网站的新手),需要写一个脚本:
- 多次收集性能计数器(在本例中为TCP)
- 计算每个计数器的rawvalue属性的连续值之间的差
规定脚本在每个间隔T迭代性能计数器N次,所述性能计数器为"A"one_answers"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
TCPv4Segments/sec is: 24723
TCPv4Connections Established is: 27
TCPv4Connections Active is: 796
TCPv4Connections Passive is: 47
TCPv4Connection Failures is: 158
TCPv4Connections Reset is: 412
TCPv4Segments Received/sec is: 14902
TCPv4Segments Sent/sec is: 9822
TCPv4Segments Retransmitted/sec is: 199
This is iteration 1
TCPv4Segments/sec is: 24727
TCPv4Connections Established is: 27
TCPv4Connections Active is: 798
TCPv4Connections Passive is: 47
TCPv4Connection Failures is: 159
TCPv4Connections Reset is: 412
TCPv4Segments Received/sec is: 14903
TCPv4Segments Sent/sec is: 9824
TCPv4Segments 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
}
}
好的。好吧,让我们处理这个然后
$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 TCPv4Segments/sec 28324837
1 TCPv4Connections Established 120
. .............................. .....
2 TCPv4Segments/sec 28325441
2 TCPv4Connections Established 125
一旦所有这些数据都被收集到$arry
中,我们就会得到所有不是第一次的迭代,并单独处理每个项目。使用管道中当前项的数据,我们将其与以前的迭代值进行匹配。使用与上面相同的值,我们得到了您希望监控的更改
Iteration Counter RawValue Change
--------- ------- -------- ------
1 TCPv4Segments/sec 28324837
1 TCPv4Connections Established 120
. .............................. .....
2 TCPv4Segments/sec 28325441 604
2 TCPv4Connections Established 125 5