Reputation: 1883
I can't figure out how to record multiple metrics from the system using a list. The $CounterList
var doesn't work but the $CounterList_Working
var does. I've seen examples that are getting a list based on the paths such as (Get-Counter -List Processor(*)).Paths
.
I thought I could specify the paths in a variable but that doesn't work.
What is wrong with the $CounterList
in the PowerShell script? The error message I get is "Get-Counter : The specified object was not found on the computer.". Which makes me think it's trying to read the list as a single value.
$CounterList = "\Network Interface(*)\Packets/sec
\Network Interface(*)\Current Bandwidth
\Network Interface(*)\Bytes Total/sec
\Memory\Committed Bytes
\Memory\Commit Limit"
$CounterList_Working = "\Processor(*)\% Processor Time"
echo "Gathering system data and writing to file $home\system_metrics.blg"
Get-Counter -Counter $CounterList -SampleInterval 2 -MaxSamples 10 | Export-counter -Path $home\system_metrics.blg
Upvotes: 4
Views: 2537
Reputation: 46690
According to the MSDN for Get-Counter, the parameter ...
-Counter
Specifies, as a string array, data from the specified performance counters. Enter one or more counter paths. You can also pipe counter path strings to this cmdlet.
You are using a single string which would be fine for a single counter. You need an array for your list of multiple counters.
$CounterList = "\Network Interface(*)\Packets/sec",
"\Network Interface(*)\Current Bandwidth",
"\Network Interface(*)\Bytes Total/sec",
"\Memory\Committed Bytes",
"\Memory\Commit Limit"
Should do the trick as would many other ways to declare a string array.
If for some crazy reason you are really working with a newline delimited string you could just convert that to a string array with a -split
. The following would give you the same results as my first example.
$CounterList = "\Network Interface(*)\Packets/sec
\Network Interface(*)\Current Bandwidth
\Network Interface(*)\Bytes Total/sec
\Memory\Committed Bytes
\Memory\Commit Limit" -split "`r`n"
However if you have any control over this I would opt for the former declaration.
Upvotes: 5