系統城裝機大師 - 唯一官網:www.farandoo.com!

當前位置:首頁 > 腳本中心 > PowerShell > 詳細頁面

PowerShell 讀取性能計數器二進制文件(.blg)記錄并匯總計算

時間:2020-01-28來源:電腦系統城作者:電腦系統城

由于監控及報告需要,要統計性能計數器每天數值情況,確認數據庫服務器的運行狀況。若打開計數器填寫,比較麻煩,現在統計用 powershell 來讀取計數器的值。

第一階段:Powershell 讀取計數器文件并統計其中一個計數器的值


 
  1. $startDate = (Get-Date).AddDays(-1).Date
  2. $endDate = (Get-Date).Date
  3. $perfPath = "D:\DataFiles\PERFMON\MSSQL_PERFMON_08240904.blg"
  4.  
  5. #讀取文件中的計數器名稱
  6. $counterList = Import-Counter -Path $perfPath
  7. $countersNameList = $counterList[0].countersamples | % {$_.path}
  8.  
  9. #篩選指定計數器和時間重新導入PS
  10. $counter = $countersNameList -like '*Processor Time*'
  11. $counterData = Import-Counter -Path $perfPath -Counter $counter | Where-Object -FilterScript {($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)}
  12.  
  13. #計算日期范圍內的數值統計
  14. $counterInfo = $counterData | Foreach-Object {$_.CounterSamples} | Measure-Object -property CookedValue -Average -Maximum
  15.  
  16. #哈希表存儲結果數據
  17. $resultTable=@{}
  18. $resultTable."CPU 利用率——平均" = $counterInfo.Average
  19. $resultTable."CPU 利用率——最大" = $counterInfo.Maximum
  20.  
  21. $resultTable

第二階段:批量統計文件中的所有計數器并導出到文件中


 
  1. $startDate = (Get-Date).AddDays(-1).Date
  2. $endDate = (Get-Date).Date
  3. $perfPath = "D:\360Downloads\*.blg"
  4.  
  5. #哈希表存儲結果數據
  6. $resultTable=@{}
  7.  
  8. #導入指定時間的所有計數器信息
  9. $counterData = Import-Counter -Path $perfPath | Where-Object -FilterScript{($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)}
  10.  
  11. #所有的計數器名字
  12. $countersNameList = $counterData[0].countersamples | % {$_.Path}
  13.  
  14. #遍歷每個計數器,將計算結果存儲到哈希表中
  15. foreach($counterName in $countersNameList)
  16. {
  17. #$counterName = "\\hzc\system\threads"
  18. $counterDataOne = $counterData | Foreach-Object {$_.CounterSamples} | Where {$_.Path-like $counterName}
  19. $counterInfo = $counterDataOne | Measure-Object CookedValue -Average -Minimum -Maximum
  20. $resultTable.$($counterName+" :平均值") = $counterInfo.Average
  21. $resultTable.$($counterName+" :最小值") = $counterInfo.Minimum
  22. $resultTable.$($counterName+" :最大值") = $counterInfo.Maximum
  23. }
  24.  
  25. #$resultTable.GetEnumerator() | sort Name | Format-Table -Auto
  26. #幾種方法導出到文件
  27. $resultTable.GetEnumerator() | sort Name | Format-Table -Auto | Out-File"D:\360Downloads\PerfmonCounter.txt"
  28. $resultTable.GetEnumerator() | sort Name | Export-Csv -Path"D:\360Downloads\PerfmonCounter.txt" -Encoding "unicode" -Force
  29. $resultTable.GetEnumerator() | sort Name | Format-List | Export-Csv -Path"D:\360Downloads\PerfmonCounter.xlsx" -Encoding "unicode" -Force

分享到:

相關信息

  • Powershell 腳本數字簽名實現方法

    腳本很容易被冒名頂替或者更改,因為它們是由純文本構成的。數字簽名為腳本提供了更高的安全性,因為它能確定腳本和腳本的編輯者的唯一性,并且不能被更改。作為腳本的發布者,你能確定你的腳本沒有被惡意篡改。即使專家也無...

    2020-01-28

  • PowerShell 語音計算器實現代碼

    帶中文發音功能的計算器程序,支持鼠標和小鍵盤輸入,支持多種數值轉人民幣的相關資料...

    2020-01-28

系統教程欄目

欄目熱門教程

人氣教程排行

站長推薦

熱門系統下載

jlzzjlzz亚洲乱熟在线播放