Posts Tagged ‘Perfmon’

I recently had a requirement to start SCA automatically on remote machines. This was needed for our stress test where we normally have a bunch of servers taking part in the test with different roles: MSSQL Server, IIS Server, File Server, etc.

Now SCA, version 5.7.2 onwards, comes with a console app, SCARunner, that allows you to run SCA from command line. That is fine if you are starting ScA from a batch file interactively, but still doesn’t solve the issue of running it remotely. Luckily PowerShell with it’s remoting and WMI capabilities is geared for just these kind of task, and helped me solve this problem.

In this blog, I’ll show you one way of accomplishing this task.

First Step

The first step is to install SCA on a remote server and then register the server within SCA. I choose the use the default hostname as the registration name since it makes it easier to use it within the PowerShell script. In the image below, my machine name is SJOSHI


Second Step

The next step is to test the SCARunner on the remote machine. To do that, you need to open a DOS/Command window in SCA installation folder which is C:\Program Files (x86)\Intergraph\SCA

To start SCARunner you need to specify these options:

-s  – The registration name. So in my case it would be SJOSHI

-f  – The folder where you want to save the files. The scan xml and perfmon .blg file

So for example, if I needed to start SCA on my machine, I would do:

SCRunner –s SJOSHI –f D:\Temp\Low


When invoked, SCARunner first runs a scan of the registered server and saves the scan xml. Then it creates a perfmon collector with the name of the machine and starts it.

To stop this collector , I need to do:

SCARunner –stop

And this will stop and remove the collector from perfmon.

Third Step

To be able to invoke this from PowerShell remotely, I need to first enable remoting on the server. This is done by running the command, Enable-PSRemoting –Force on the server from a PowerShell admin prompt.



Fourth Step

So at this stage we have the server configured (SCA & PowerShell) and we know the options to run SCARunner. The remaining part is to work out the PowerShell script, so let’s tackle that now.

PowerShell with its WMI Cmdlets, allows you to invoke methods against WMI classes. In our case, we need to start a process, SCARunner, so the Win32_Process with it’s Create method looks like an ideal candidate.

To invoke a WMI method, we can use the Invoke-CimMethod Cmdlet which takes the class name, method name and an arguments dictionary. So let’s see how you can use it.

Say I want to open a file, test.txt in my D:\Temp\Low folder using this class. I can do this in command line by typing notepad D:\Temp\Low\Test.txt


And the file does open up. To do the same in PowerShell, you need to type:

Invoke-CimMethod –ClassName Win32_Process –MethodName Create –Arguments @{CommandLine=“notepad D:\Temp\Low\Test.txt”}

And notepad does open up:


What’s with the –Arguments field ? Well the Get-Help on Invoke-CimMethod shows it to be a dictionary (key-value type).


So it takes, a key and a value. The key name is CommandLine and value is the path to exe with arguments if reqd. The key name is obtained from the Create method description as shown here


So we can get notepad to open via WMI. Now lets try it against a remote-machine. If you do a Get-Help Invoke-CimMethod in PowerShell, you will see that it takes a –ComputerName or –CimSession parameter.


So type the same command with an additional –ComputerName option followed by a machine name that you have for this test:

Invoke-CimMethod –ComputerName XYZ ..rest of the command as before.

As you can see from the image below, the remote process is created and shown under Task Manager. Do note, the remote processes don’t show a UI because of security reasons.



So let’s now try with the SCARunner application against a remote machine on which SCA is installed (and server registered). Everything will be same as before except the CommandLine, which should now include the path to SCARunner along with the start-up options:



And you can see that it does start successfully which is pretty cool!

To stop it, you can just use the –stop option, in the $cmdLine variable:

$cmdLine = "C:\Program Files (x86)\Intergraph\SCA\SCARunner.exe -stop"

In my setup, I use a CSV file to import a list of servers using the Import-Csv cmdlet, and startup SCA on those machines.


Hope this motivates you to use PowerShell to automate remote-tasks which would otherwise be hard to accomplish in a simple and efficient way.

Read Full Post »

If you have used PowerShell V2 (and above) for sometime now, you would know that you can use the Import-Counter Cmdlet to read Perfmon data from a .blg file.

This works fine when you want to look at data “as it is” and export to a CSV file or format it for display. I recently ran into a scenario where I had parse multiple perfmon files and then “look” at the data with different time intervals for a few specific counters only.

PS >dir D:\Sunit\Blog\PerfmonFiles -Include *.blg -Recurse | Select Fullname






Also I needed to dump all the data into one CSV file so that I could use that for further analysis. So I wrote a function to allow me to do this and is included here in case it’s useful for someone else too.

The crux of the script is in this section where I calculate the averages depending on the interval:

$data = Import-Counter -Path $Path -Counter $counters
$d = $data | Where {$_.countersamples.status -eq 0}
$entries = @()
$runID = ($Path -split "\\")[-2] #Change this to your depth
Write-Debug "Key $runID"
for($i=1; $i -lt $d.Count; $i += $Interval)
$UBound = $i + ($Interval-1)
$entries += [pscustomobject] @{
Timestamp = ([datetime] $d[$i].Timestamp).ToLongTimeString()
CPUAvgProc = $d[$i..$UBound] | Select -ExpandProperty CounterSamples | `
Where {$_.Path -like $counters[0]} | measure -Property CookedValue -Average `
| select -ExpandProperty Average
CPUAvgPriv = $d[$i..$UBound] | Select -ExpandProperty CounterSamples | Where {$_.Path -like $counters[1]} `
| measure -Property CookedValue -Average | select -ExpandProperty Average
RunID = $runID

The script takes in pipeline input (from dir cmd) and lets you specify the interval and CSVFolder to dump individual files to. In my case the logging interval was 15 secs, so if I specified –Interval 4, it would average data for every 4 counters and display it in each minute interval.

It also has a –Summary option that let’s you see the logging-interval and the counter paths in the perfmon file. Something like this:


To aggregate the data for multiple files and place all data in one file, you would do:

dir D:\Sunit\Blog\ParseBLGFiles -Include *.blg –Recurse | ParseBLGFile -Interval 4 –AllInOneFile

Here’s a run with interval specified as 4 (data is avg. for each min since logging interval is 15 sec in this case) and option to save all of the data in one csv file:

dir D:\Sunit\Blog\PerfmonFiles -Include *.blg -Recurse | ParseBLGFile -Interval 4 -AllInOneFile –CSVFolder D:\Sunit\Blog\PerfmonFiles | ft -AutoSize


And the subsequent CSV file, AllRuns.csv


Here’s the entire script (rename to .zip). If it proves helpful, do please let me know.


Read Full Post »