通过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 2010-11-04 11:56  stswordman  阅读(1352)  评论(0编辑  收藏  举报