I'm trying to set up a script that generates blg files every set amount of time, using powershell. I know this can be done using perfmon XML templates, but for this specific project I have to do it with powershell if at all possible.
My main issue is that I am unable to store the list of performance counters I want to use in a variable and reuse them.
I've attempted to use the following script to create a list of performance counters to use:
$Counters = @()
$Counters += '\Memory\Available Bytes'
$counters += '\Paging File(*)\% Usage'
$Counters += '\PhysicalDisk(*)\Disk Reads/sec'
$Counters += '\PhysicalDisk(*)\Disk Writes/sec'
$Counters += '\PhysicalDisk(*)\Avg. Disk sec/Read'
$Counters += '\PhysicalDisk(*)\Avg. Disk sec/Write'
$Counters += '\Processor(*)\% Processor Time'
$Counters += '\System\Processor Queue Length'
foreach($counter in $Counters)
{
$string = $string + ", '" + $counter + "'"
$string = $string.TrimStart(",")
}
if I then proceed to use get-counter $string I get the following error:
Get-Counter : The specified counter path could not be interpreted.
Yet when I copy the exact value of the string, and use get-counter -counter Values of $string it works fine...
Could someone advise me on a way to get get-counter to work with either an array or a string with a list of counters?
When I use your
+=
block, I get back a$counters
that is a long concatenated string.That probably isn't what you want. If you make
$counters
an array explicitly things work a little better.