通过powershell读取性能计数器
读取指定文件夹里性能计数器文件的信息,取出各个counter的平均值,存储在csv
param($folderPath="")
function funhelp()
{
$helpText=@"
NAME: PerformanceCounterCalculation)_multipleFile.ps1
This script will loop all the counters and calcualte the average value
PARAMETERS:
-$folderPath Specifies the folder of the .BLG you want to get
SYNTAX:
PerformanceCounterCalculation)_multipleFile.ps1 -folderPath "c:\disk\*"
"@
Write-Host $helpText -ForegroundColor Green
exit
}
#define PerformanceCounter CLASS
$source = @"
public class PerformanceCounter
{
private string computerName;
private string counterPath;
private double value;
private string date;
public PerformanceCounter(string computerName,string counterPath, double value,string date)
{
this.computerName = computerName;
this.counterPath = counterPath;
this.value = value;
this.date=date;
}
public double Value
{
get { return this.value; }
}
public string CounterPath
{
get { return this.counterPath; }
}
public string ComputerName
{
get { return this.computerName; }
}
public string Date
{
get { return this.date; }
}
}
"@
#------------------------------------------------------This script will loop all the counters and calcualte the average value
function Cal([string]$filename)
{
$PathCount=0;
$CounterArray=import-counter $filename
$PathCount=$CounterArray[0].CounterSamples.Count
for($i=0;$i -lt $PathCount;$i++)
{
$total=0
foreach ($singleCounterArray in $CounterArray)
{
$total=$total+$singleCounterArray.CounterSamples[$i].CookedValue
}
$averageValue= $total/$CounterArray.count
$countPath=$CounterArray[0].CounterSamples[$i].Path;
$countPath=$countPath.Substring(2,$countPath.Length-2);#elminiate the first \\
$computerName=$countPath.Substring(0,$countPath.IndexOf("\"));#get the computername
$countPath=$countPath.Substring($countPath.IndexOf("\"),$countPath.Length-$countPath.IndexOf("\"));#remove the computername
$date=[string]$CounterArray[0].Timestamp.ToString("yyyyMMdd");
$counter=New-Object PerformanceCounter($computerName,$countPath,$averageValue,$date);
$c=$a.add($counter) # $.add operation will output the number of PerformanceCounter, add $c to elimiate the behavior
}
}
if ($folderPath -eq "")#$folderPath can't be empty
{
funhelp
exit
}
$a=New-Object System.Collections.ArrayList
Add-Type -TypeDefinition $source
$blgList=Get-Item $folderPath -include *.blg
$processedNumber=0
foreach($blg in $blgList)
{
$processedNumber=$processedNumber+1
Cal([string]$blg.FullName)
Write-Host $processedNumber "files of " $blgList.count "have been processed"
}
Write-Host "Process done, Please wait..." -ForegroundColor Green
$filename="result"+[system.datetime]::now.ToString("yyyyMMddhhmmss")+".csv"
$a |Export-Csv $filename
Write-Host "Please check" $filename "for detail" -ForegroundColor Green
function funhelp()
{
$helpText=@"
NAME: PerformanceCounterCalculation)_multipleFile.ps1
This script will loop all the counters and calcualte the average value
PARAMETERS:
-$folderPath Specifies the folder of the .BLG you want to get
SYNTAX:
PerformanceCounterCalculation)_multipleFile.ps1 -folderPath "c:\disk\*"
"@
Write-Host $helpText -ForegroundColor Green
exit
}
#define PerformanceCounter CLASS
$source = @"
public class PerformanceCounter
{
private string computerName;
private string counterPath;
private double value;
private string date;
public PerformanceCounter(string computerName,string counterPath, double value,string date)
{
this.computerName = computerName;
this.counterPath = counterPath;
this.value = value;
this.date=date;
}
public double Value
{
get { return this.value; }
}
public string CounterPath
{
get { return this.counterPath; }
}
public string ComputerName
{
get { return this.computerName; }
}
public string Date
{
get { return this.date; }
}
}
"@
#------------------------------------------------------This script will loop all the counters and calcualte the average value
function Cal([string]$filename)
{
$PathCount=0;
$CounterArray=import-counter $filename
$PathCount=$CounterArray[0].CounterSamples.Count
for($i=0;$i -lt $PathCount;$i++)
{
$total=0
foreach ($singleCounterArray in $CounterArray)
{
$total=$total+$singleCounterArray.CounterSamples[$i].CookedValue
}
$averageValue= $total/$CounterArray.count
$countPath=$CounterArray[0].CounterSamples[$i].Path;
$countPath=$countPath.Substring(2,$countPath.Length-2);#elminiate the first \\
$computerName=$countPath.Substring(0,$countPath.IndexOf("\"));#get the computername
$countPath=$countPath.Substring($countPath.IndexOf("\"),$countPath.Length-$countPath.IndexOf("\"));#remove the computername
$date=[string]$CounterArray[0].Timestamp.ToString("yyyyMMdd");
$counter=New-Object PerformanceCounter($computerName,$countPath,$averageValue,$date);
$c=$a.add($counter) # $.add operation will output the number of PerformanceCounter, add $c to elimiate the behavior
}
}
if ($folderPath -eq "")#$folderPath can't be empty
{
funhelp
exit
}
$a=New-Object System.Collections.ArrayList
Add-Type -TypeDefinition $source
$blgList=Get-Item $folderPath -include *.blg
$processedNumber=0
foreach($blg in $blgList)
{
$processedNumber=$processedNumber+1
Cal([string]$blg.FullName)
Write-Host $processedNumber "files of " $blgList.count "have been processed"
}
Write-Host "Process done, Please wait..." -ForegroundColor Green
$filename="result"+[system.datetime]::now.ToString("yyyyMMddhhmmss")+".csv"
$a |Export-Csv $filename
Write-Host "Please check" $filename "for detail" -ForegroundColor Green
下面是csv文件的效果图
1
2
3
分类:
sql
, powershell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现