通过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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值