PowerShell 读取性能计数器二进制文件(.blg)记录并汇总计算

所属分类: 脚本专栏 / PowerShell 阅读数: 2005
收藏 0 赞 0 分享

由于监控及报告需要,要统计性能计数器每天数值情况,确认数据库服务器的运行状况。若打开计数器填写,比较麻烦,现在统计用 powershell 来读取计数器的值。

第一阶段:Powershell 读取计数器文件并统计其中一个计数器的值

$startDate = (Get-Date).AddDays(-1).Date 
$endDate = (Get-Date).Date 
$perfPath = "D:\DataFiles\PERFMON\MSSQL_PERFMON_08240904.blg" 
 
#读取文件中的计数器名称 
$counterList = Import-Counter -Path $perfPath 
$countersNameList = $counterList[0].countersamples | % {$_.path} 
 
#筛选指定计数器和时间重新导入PS 
$counter = $countersNameList -like '*Processor Time*' 
$counterData = Import-Counter -Path $perfPath -Counter $counter | Where-Object -FilterScript {($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)}  
 
#计算日期范围内的数值统计 
$counterInfo = $counterData | Foreach-Object {$_.CounterSamples} | Measure-Object -property CookedValue -Average -Maximum 
 
#哈希表存储结果数据 
$resultTable=@{} 
$resultTable."CPU 利用率——平均" = $counterInfo.Average 
$resultTable."CPU 利用率——最大" = $counterInfo.Maximum 
 
$resultTable 

第二阶段:批量统计文件中的所有计数器并导出到文件中

$startDate = (Get-Date).AddDays(-1).Date  
$endDate = (Get-Date).Date  
$perfPath = "D:\360Downloads\*.blg" 
 
#哈希表存储结果数据  
$resultTable=@{} 
 
#导入指定时间的所有计数器信息 
$counterData = Import-Counter -Path $perfPath | Where-Object -FilterScript {($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)} 
 
#所有的计数器名字 
$countersNameList = $counterData[0].countersamples | % {$_.Path} 
 
#遍历每个计数器,将计算结果存储到哈希表中 
foreach($counterName in $countersNameList)  
{  
#$counterName = "\\hzc\system\threads" 
$counterDataOne = $counterData | Foreach-Object {$_.CounterSamples} | Where {$_.Path -like $counterName}  
$counterInfo = $counterDataOne | Measure-Object CookedValue -Average -Minimum -Maximum 
$resultTable.$($counterName+" :平均值") = $counterInfo.Average 
$resultTable.$($counterName+" :最小值") = $counterInfo.Minimum 
$resultTable.$($counterName+" :最大值") = $counterInfo.Maximum 
} 
 
#$resultTable.GetEnumerator() | sort Name | Format-Table -Auto 
#几种方法导出到文件 
$resultTable.GetEnumerator() | sort Name | Format-Table -Auto | Out-File "D:\360Downloads\PerfmonCounter.txt" 
$resultTable.GetEnumerator() | sort Name | Export-Csv -Path "D:\360Downloads\PerfmonCounter.txt" -Encoding "unicode" -Force 
$resultTable.GetEnumerator() | sort Name | Format-List | Export-Csv -Path "D:\360Downloads\PerfmonCounter.xlsx" -Encoding "unicode" -Force 

更多精彩内容其他人还在看

Powershell学习笔记--使用正则表达式查找文件

本文介绍PowerShell中使用正则表达式的查找文件的方法,PowerShell的正则表达式与微软其它语言的正则表达式是一样的,使用非常方便。
收藏 0 赞 0 分享

Windows Powershell 介绍和安装

Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境。微软之所以将Powershell 定位为Power,并不是夸大其词,因为它完全支持对象。其可读性,易用性,可以位居当前所有shell之首。
收藏 0 赞 0 分享

Windows Powershell 自定义控制台

这篇文章主要介绍了Windows Powershell 自定义控制台,包括选项、字体、布局和颜色四个方面的自定义风格,希望对大家有所帮助
收藏 0 赞 0 分享

windows Powershell 快速编辑模式和标准模式

powershell控制台有两种模式,一个是快速编辑模式,一个是标准模式。
收藏 0 赞 0 分享

PowerShell中查找字符串位置的IndexOf函数使用实例

这篇文章主要介绍了PowerShell中查找字符串位置的IndexOf函数使用实例,例子简单明了,容易看懂,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell使用正则表达式查找字符串实例

这篇文章主要介绍了PowerShell使用正则表达式查找字符串实例,主要是对match运算符的使用介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中使用正则表达式跨行匹配字符串的方法

这篇文章主要介绍了PowerShell中使用正则表达式跨行匹配字符串的方法,重点在于正则表达式的写法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell中查看当前版本、Windows版本、.NET版本信息的代码

这篇文章主要介绍了PowerShell中查看当前版本、Windows版本、.NET版本信息的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 入门基础教程

Windows PowerShell 是专为系统管理员设计的新 Windows 命令行外壳程序。该外壳程序包括交互式提示和脚本环境,两者既可以独立使用也可以组合使用
收藏 0 赞 0 分享

PowerShell Contains函数查找字符串实例

这篇文章主要介绍了PowerShell Contains函数查找字符串实例,Contains函数的作用是查询一个字符串中是否存在另一个字符串,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多