Get-Counter -Counter $Counters -MaxSamples 30 | ForEach

Shubhi Jain 6 Reputation points
2022-01-13T07:48:14.333+00:00

$serverName = "DESKTOP-S19MG83\DEVMSSQL"
$Counters = @(
("\$serverName" +"\Process(sqlservr*)\% User Time"),
("\$serverName" +"\Process(sqlservr*)\% Privileged Time")
)
Get-Counter -Counter $Counters -MaxSamples 30 | ForEach {
$.CounterSamples | ForEach {
[pscustomobject]@{
TimeStamp = $
.TimeStamp
Path = $.Path
Value = ([Math]::Round($
.CookedValue, 3))
}
Start-Sleep -s 2
}
}

After Running this am getting the below error.

Get-Counter : Unable to connect to the specified computer or the computer is offline.
At line:6 char:5

  • Get-Counter -Counter $Counters -MaxSamples 30 | ForEach {
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidResult: (:) [Get-Counter], Exception
  • FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

Get-Counter : Unable to connect to the specified computer or the computer is offline.
At line:6 char:5

  • Get-Counter -Counter $Counters -MaxSamples 30 | ForEach {
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidResult: (:) [Get-Counter], Exception
  • FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

System is online, firewall is off so what's the issue?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Milotin 6 Reputation points
    2022-01-13T07:53:33.707+00:00

    Hi,

    Your ServerName variable is wrong. DESKTOP-S19MG83 would be your server name, i would assume DEVMSSQL is just the SQL Instance.
    You are also missing a "" on line 7. should be $.CounterSamples instead of $.CounterSamples.

    Try it this way
    164711-troubleshoot-highcpu.txt
    164683-image.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.