Friday, July 6, 2012

How to use Powershell to manage Counter Logs

After having written this post about using Powershell to deploy and start Counter Logs, I decided to go further and extends the same script to allow two more actions, that is:
  • stop collecting performance data on multiple server
  • remove performance data collection from multiple servers
As in the previous script configuration, the stop/remove operation is executed on a list of computers which is stored in a text file. The other required parameter for the stop/remove operation is the name of the Data Collector Set. The stop and remove operations are in fact methods of the interface DataCollectorSet which is used to manage performance data collections.

In my case the name of the Data Collection is always 'perf_$server', where $server is the name of the server recursively retrieved for the host list.


Here's my piece of code. Feel free to use it and modify it to suit your neeeds.
$task = "deploy" # possible values: "deploy","start","stop","delete"
$PWD = "c:\rootfolder"
$counters = @(Get-Content $pwd\perfmon_counters.txt)
$servers = @(Get-Content $pwd\perfmon_list.txt)
#$frequence = "00:00:02" #hh:mm:ss high frequence polling
#$frequence = "00:00:30" #hh:mm:ss medium frequence polling
$frequence = "00:02:00" #hh:mm:ss low frequence polling
$logtype = "bincirc" # possible value: 
$max_duration = "168:00:00"
$max_size = "50" # megabytes
foreach ($server in $servers)
{
if($task -like "deploy"){
$counters | % {$_ -replace "", "\\$server"} | Set-Content $pwd\$server.txt
$strCMD1 = "C:\Windows\System32\logman.exe create counter perf_$server -s $server -si $frequence -cf $pwd\$server.txt -f $logtype -v mmddhhmm -o C:\PerfLogs\log_$server -rf $max_duration -max $max_size -u adminaccount adminpassword"
Invoke-Expression $strCMD1
Remove-Item $pwd\$server.txt
}
if($task -like "start"){
$datacollectorset = new-object -COM Pla.DataCollectorSet  
$datacollectorset.Query("perf_$server",$server)
$datacollectorset.start($false)
}
if($task -like "stop"){
$datacollectorset = new-object -COM Pla.DataCollectorSet  
$datacollectorset.Query("perf_$server",$server)
$datacollectorset.stop($false)
}
if($task -like "delete"){
$datacollectorset = new-object -COM Pla.DataCollectorSet  
$datacollectorset.Query("perf_$server",$server)
$datacollectorset.delete()
}
}
If you are deploying your performance counters on Windows 2008 or Windows 7, you can also rewrite the first 'deploy' task to use a 'datacollectorset' and set the configuration using an XML template. Check here for more information on how to create a Data Collector Set from a template. Unfortunately Windows 2003 does not support this method, so I had to stick to old good Logman to get the task done.... but of course I am open to suggestions!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...