Powershell

时间:2016-08-24 11:52:38

标签: sql-server powershell performancecounter

我已经借用了以下一些代码,但我没有得到我预期的输出。最后,我想为每个包含最后x个值的计数器创建一个列表/数组。 但我预计$obj将是我可以逐步执行的计数器列表,下面的代码会给出一个在每个计数器之间收集***的输出,但事实并非如此?

如何依次循环每个计数器并获得其值?

$counters = get-counter -ComputerName sql -Counter "\SQLServer:General Statistics\User Connections","\SQLServer:Memory Manager\Memory Grants Pending","\SQLServer:SQL Statistics\batch requests/sec","\SQLServer:SQL Statistics\sql compilations/sec","\SQLServer:SQL Statistics\sql re-compilations/sec" -SampleInterval 1

foreach($metric in $counters)            
{            
  $obj = $metric.CounterSamples | Select-Object -Property Path, CookedValue;            

  write-host $obj 
  write-host "***"
}  

我得到的当前输出如下:

@{Path=\\sql\\sqlserver:general statistics\user connections; CookedValue=96} @{Path=\\sql\\sqlserver:memory manager\memory grants pending; CookedValue=0} @{Path=\\sql\\sqlserver:sql statistics\batch requests/sec; CookedValue=110.969626692457} @{Path=\\sql\\sqlserver:sql statistics\sql compilations/sec; CookedValue=17.9950745987768} @{Path=\\sql\\sqlserver:sql statistics\sql re-compilations/sec; CookedValue=5.99835819959228}
***

我希望更像这样:

Counters 1 = 0.5
***
Counter 2 = 1.2
*** 

这会将输出分解为单独的计数器,但我想我现在理解这些评论。

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?

$counters = $(Get-Counter -Counter '\Processor(_Total)\% Processor Time', '\LogicalDisk(_Total)\% Free Space').CounterSamples | Select-Object -Property Path, CookedValue

foreach($obj in $counters)            
{            
  Write-Host -Object '***'
  $obj = $($obj | ConvertTo-Json|
      ForEach-Object -Process {
        [System.Text.RegularExpressions.Regex]::Unescape($_)
      }) -replace ‘[{},\n]’
  Write-Host -Object $obj
  Write-Host -Object '***'
}