# Powershell: Problems with Get-Counter cmdlet

• ### Question

• Hey there,

i've been writing some code to gather Perfmon counters with Powershell. The counters are stored in a XML file.
If I write something like

Get-Counter -Counter $CounterNames -MaxSamples$sampint | Export-Counter -Path $Env:TMP\System_Counters.blg -Force;  Where$CounterNames is an array produced by

for ($i = 0;$i -lt ($CounterXML.region.OS[0].Counter).Count;$i++)
{
$CounterNames +=$CounterXML.region.OS[0].Counter[$i].Value$j = $i + 1 if ($j -lt ($CounterXML.region.OS[0].Counter).Count) {$CounterNames += ','
}
else {}

}

it won't work. I always recieve this error:

Get-Counter : The specified counter path could not be interpreted.
At C:\Users\blabla.ps1:221 char:6
+                     Get-Counter -Counter $CounterNames -MaxSamples$sampint | Export-Counter -P ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

But if I use the value of $CounterNames, which is ('\Process(_Total)\% Processor Time','\Memory\Available Bytes'), it works and I don't see why. I also checked the types of the array and the of braces, they are equal. Then, I went to Microsoft Docs (Link below), tried Example 8 and it worked. After a little experimenting I noticed that the type of the$MemCounters is a StringCollection. But that didn't work either...

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.diagnostics/get-counter?view=powershell-4.0

Has anyone an idea what I am doing wrong? I wasted several days with Google and Forums but i didn't find anything that could help.

Btw, I am using Powershell 4 and Windows 8.1 Pro

Thursday, August 17, 2017 12:45 PM

• With a correctly designed schema you can do this:

[xml]$xml= @' <region name="Counters"> <OS Lang= "English"> <Counter ComputerName="alpha" Name="\\alpha\Process(_Total)\% Processor Time"/> <Counter ComputerName="alpha" Name="\\alpha\Memory\Available Bytes"/> <Counter ComputerName="localhost" Name="\Memory\Available Bytes"/> </OS> </region> '@ Get-Counter$xml.SelectNodes('//OS[@Lang="English"]/Counter[@ComputerName="alpha"]').Name

\_(ツ)_/

• Edited by Thursday, August 17, 2017 1:41 PM
• Marked as answer by Friday, August 18, 2017 6:02 AM
Thursday, August 17, 2017 1:37 PM

### All replies

• Test each counter name to be sure they are all specified correctly.  The names are the full path to the counter.

\_(ツ)_/

Thursday, August 17, 2017 12:53 PM
• They are correct. I just added the computername to the paths, but it doesn't make a difference..
Thursday, August 17, 2017 12:59 PM
• The file you are outputting is NOT xml. It is proprietary binary format.

For a basic explanation of these files see: https://www.reviversoft.com/file-extensions/blg

\_(ツ)_/

Thursday, August 17, 2017 1:00 PM
• They are correct. I just added the computername to the paths, but it doesn't make a difference..

Test them one at a time.

The names must be in an array and not just a string.

\_(ツ)_/

Thursday, August 17, 2017 1:02 PM
• Note that the counters must exist (have instances).

$counters = Get-Counter -ListSet processor|select -Expand PathsWithInstances Get-Counter$counters

\_(ツ)_/

Thursday, August 17, 2017 1:04 PM
• yes, I don't want to output a XML. the .blg file will be used for backup of the counters
Thursday, August 17, 2017 1:04 PM
• I have no idea what is in your XML.  It probably is not constructed so it can be read that way.

\_(ツ)_/

Thursday, August 17, 2017 1:08 PM
• The name collector needs to create an array.  It is creating a comma separated string.

\_(ツ)_/

Thursday, August 17, 2017 1:09 PM
• This creates an array:

$CounterNames = @() for ($i = 0; $i -lt ($CounterXML.region.OS[0].Counter).Count; $i++) {$CounterNames += $CounterXML.region.OS[0].Counter[$i].Value
}

\_(ツ)_/

Thursday, August 17, 2017 1:11 PM
• This should also create an array:

$counterNames =$CounterXML.region.OS[0].Counter.Value

\_(ツ)_/

Thursday, August 17, 2017 1:12 PM
• I tested them as you said and it worked.(?!) can you explain that?

Here's my xml:

<region name="Counters">
<OS Lang= "English">
<Counter>
<Value>"\\computer\Process(_Total)\% Processor Time"</Value>
</Counter>
<Counter>
<Value>"\\computer\Memory\Available Bytes"</Value>
</Counter>
</OS>
</region>

but i can get the values from the xml without a problem

Thursday, August 17, 2017 1:14 PM
• the for-loop you wrote gives me the exact same error
Thursday, August 17, 2017 1:17 PM
• You cannot put quotes around the counter names.

\_(ツ)_/

Thursday, August 17, 2017 1:17 PM
• without the quotes i get the following error:

Get-Counter : Unable to connect to the specified computer or the computer is offline.
At C:\Users\blabla.ps1:227 char:6
+                     Get-Counter -Counter $CounterNames -MaxSamples$sampint | Export-Counter -P ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

Thursday, August 17, 2017 1:20 PM
• Example:

[xml]$xml= @' <region name="Counters"> <OS Lang= "English"> <Counter> <!-- With computer name --> <Value>\\alpha\Process(_Total)\% Processor Time</Value> </Counter> <Counter> <!-- WITHOUT computer name --> <Value>\Memory\Available Bytes</Value> </Counter> </OS> </region> '@ Get-Counter$xml.region.OS.Counter.ValueORGet-Counter $xml.region.OS[0].Counter.Value \_(ツ)_/ • Edited by Thursday, August 17, 2017 1:22 PM Thursday, August 17, 2017 1:21 PM • You can also do a full query: Get-Counter$xml.SelectSingleNode('//OS[@Lang="English"]').Counter.Value

\_(ツ)_/

Thursday, August 17, 2017 1:25 PM
• This would be a more correct and useful XML schema.

[xml]$xml= @' <region name="Counters"> <OS Lang= "English"> <Counter Name="\\alpha\Process(_Total)\% Processor Time"/> <Counter Name="\Memory\Available Bytes"/> </OS> </region> '@ Get-Counter$xml.SelectSingleNode('//OS[@Lang="English"]').Counter.Name


\_(ツ)_/

Thursday, August 17, 2017 1:29 PM
• that might be, i am a newbie to xml

but anyways, i still get errors like

Get-Counter : Unable to connect to the specified computer or the computer is offline.
+                     Get-Counter -Counter $xml.region.OS.Counter.Value -MaxSamples$sampint | Ex ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

tomorrow i'll try again

Thursday, August 17, 2017 1:36 PM
• With a correctly designed schema you can do this:

[xml]$xml= @' <region name="Counters"> <OS Lang= "English"> <Counter ComputerName="alpha" Name="\\alpha\Process(_Total)\% Processor Time"/> <Counter ComputerName="alpha" Name="\\alpha\Memory\Available Bytes"/> <Counter ComputerName="localhost" Name="\Memory\Available Bytes"/> </OS> </region> '@ Get-Counter$xml.SelectNodes('//OS[@Lang="English"]/Counter[@ComputerName="alpha"]').Name

\_(ツ)_/

• Edited by Thursday, August 17, 2017 1:41 PM
• Marked as answer by Friday, August 18, 2017 6:02 AM
Thursday, August 17, 2017 1:37 PM
• that might be, i am a newbie to xml

but anyways, i still get errors like

Get-Counter : Unable to connect to the specified computer or the computer is offline.
+                     Get-Counter -Counter $xml.region.OS.Counter.Value -MaxSamples$sampint | Ex ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
+ FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

tomorrow i'll try again

Different error.  The computer  is offline or the management ports are blocked.

You must carefully read all of the error.

\_(ツ)_/

Thursday, August 17, 2017 1:42 PM
• Thank you so much!!

Get-Counter \$xml.SelectNodes('//OS[@Lang="English"]/Counter[@ComputerName="alpha"]').Name