Using Logman to Start Perfmon with PowerShell

July 21, 2017    DevOps PowerShell

Using Logman to Start Perfmon with PowerShell

We have a lot of VMs to run all of our Selenium UI tests in parallel. We were seeing some odd things (it seems like I’m always looking into timing issues or other issues with these UI tests. That’s a good reason to write more unit tests than UI tests and follow the Testing Pyramid) like blank browser screens and loading indicators not going away.

We wanted to collect VM metrics to get a better idea if there were usage spikes in resources. So instead of going on all the VMs to add a Permon monitoring, I spent a few hours putting together a PowerShell script from a few blog posts I found.

I followed parts of Collen Morrow’s article and then used the documentation to understand the options and apply them correctly.

The 2 minute drill was also very helpful.

This script will create a perfmon log using logman.exe. It will start the data collector and run for the specified time and query every 2 minutes (or what you pass in to $gatherEvery).

These were all helpful commands to run while testing this out.

logman query

logman start name

logmand stop name

logman delete name

    [string]$timeToCollectDataFor = "06:00:00",
    [string]$resultsDirectory = "C:\PerfLogs",
    [string]$gatherEvery = "00:02:00"

if (-not (Test-Path $resultsDirectory))
    Write-Output "Creating $resultsDirectory directory."
    New-Item -Path $resultsDirectory -ItemType Directory

# appending to create a file that starts with perfLog_{-v}, otherwise the file is put in c:\ for some reason.
$resultsDirectory += "\perfLog";

$dataCollectorSetName = "My-Perf-Counter-Log";
Write-Output "Writing output to $outputTo";

# is the collector already there?
$logmanQuery = Invoke-Expression "logman query $dataCollectorSetName";

if(!($logmanQuery -Match $dataCollectorSetName)){
    Write-Output "Creating $dataCollectorSetName";

    # every $gatherEvery for $timeToCollectDataFor, --r = no repeat
    $strCMD = "logman create counter $dataCollectorSetName -f csv -v mmddhhmm -max 250 -si $gatherEvery --r -rf $timeToCollectDataFor -o  $resultsDirectory ";
    $strCMD += "-c '\LogicalDisk(*)\Avg. Disk Queue Length', '\LogicalDisk(*)\Avg. Disk sec\Transfer', '\Process(*)\% Processor Time'";
    # $strCMD += "-c '\LogicalDisk(*)\*' '\Memory\*' '\Network Interface(*)\*' '\Paging File(*)\*' '\PhysicalDisk(*)\*' '\Process(*)\*' '\Redirector\*' '\Server\*' '\System\*'"
    Write-Output $strCMD
    Invoke-Expression $strCMD

if($logmanQuery -Match "Running"){
    Write-Output "Stopping the counter"
    Invoke-Expression "logman stop $dataCollectorSetName";

Write-Output "starting the counter"
Invoke-Expression "logman start $dataCollectorSetName";

Invoke-Expression "logman query $dataCollectorSetName";

The next challenge is to collect all these .csv files and combine them without having to do it manually.

I’ve created a script to stop the counter then copy those files to a network drive for easy retrieval.

# Stop the Perfmon collector, grab from $resultsDirectory (needs to match the CreateAndStart script directory)

    [string]$dataCollectorSetName = "My-Perf-Counter-Log"

$logmanQuery = Invoke-Expression "logman query $dataCollectorSetName";

if($logmanQuery -Match "Running"){
    Write-Output "Data Collector is running";
    Write-Output "Stopping the Data Collector";

    Invoke-Expression "logman stop $dataCollectorSetName";

[string]$collectorPath = $logmanQuery | Select-String "Output Location:";
$collectorPath = $collectorPath.substring(22);
Write-Output "Root path of data collector is $collectorPath";

# copy all csv files to \\{network-dirve}\share\Perfmon VM Data
# create folder
$perfmonDataFolder = "\\{network-dirve}\share\Perfmon VM Data\";
$now = Date
$destinationFolder = $perfmonDataFolder + $now.DayOfYear + $now.Hour;
if (-not (Test-Path $destinationFolder))
    Write-Output "Creating $destinationFolder"
    New-Item -Path $destinationFolder -ItemType Directory

Copy-Item -Path $collectorPath -Destination $destinationFolder -Recurse

comments powered by Disqus

Please consider using Brave and adding me to your payment ledger. Then you won't have to see ads!

Support me and download Brave!

Use Brave