随笔 - 229  文章 - 2  评论 - 511  阅读 - 84万

通过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
复制代码

 

 

 

下面是csv文件的效果图

1

2

3

posted on   stswordman  阅读(1360)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2010年11月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示