Method invocation failed because [System.Object[]] does not contain a method named 'Substring'.

Yonglong YL44 Wang 61 Reputation points
2021-10-29T15:16:38.113+00:00

I want to write a powershell script to get the java daemon , when it's over 30(%) or 40%, i will restart some java application, here is my script.

$java_used_cpu_string = Get-Counter -ErrorAction SilentlyContinue '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples| Select-Object -Property instancename, cookedvalue| ? {$_.instanceName -notmatch "^(idle|total|system)$"} | Sort-Object -Property cookedvalue -Descending| Select-Object -First 5| ft InstanceName,@{L='CPU';E={($.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize |findstr java
$java_used_cpu1 = $java_used_cpu_string -replace "^.+?\s+(.+)$",'$1'
$java_used_cpu = $java_used_cpu1.Substring(0,$java_used_cpu1.Length-1)

$num_java_used_cpu = [int]$java_used_cpu

but the got the following error.

Method invocation failed because [System.Object[]] does not contain a method named 'Substring'.
At D:\ProgramFiles86\ValidateAddressWs\bin\restart_dma_cpu_high.ps1:4 char:1

  • $java_used_cpu = $java_used_cpu1.Substring(0,$java_used_cpu1.Length- ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : MethodNotFound
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2021-10-29T19:30:33.847+00:00

    I think you can reduce that code quite a bit:

    $num_java_used_cpu = Get-Counter -ErrorAction SilentlyContinue '\Process(*)\% Processor Time' | 
        Select-Object -ExpandProperty countersamples |          # get array of PerformanceCounterSample objects
            Where-Object {$_.InstanceName -eq 'java'} |         # get just the counters for java        
                Sort-Object -Property cookedvalue -Descending | # sort by cpu consumer
                    Select-Object InstanceName, @{L = 'CPU'; E = { [int]($_.Cookedvalue/$env:NUMBER_OF_PROCESSORS)} } -First 1  # get the largest CPU consumer
    
    Format-Table $num_java_used_cpu
    

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 48,046 Reputation points
    2021-10-29T18:03:55.893+00:00

    $java_used_cpu1 is an array, not a string, hence the error.

    Get-Counter will return back multiple results. Therefore your $java_used_cpu_string is an array. Using findstr just filters that array down. Using replace on the next line runs it for each array element. At this point you have 0 or more possible values.

    If you only ever expect a single value to come back from the Get-Counter call then after the findstr call pipe to select-object -first 1.

    $java_used_cpu_string = Get-Counter -ErrorAction SilentlyContinue '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples| Select-Object -Property instancename, cookedvalue| ? {$_.instanceName -notmatch "^(idle|total|system)$"} | Sort-Object -Property cookedvalue -Descending| Select-Object -First 5| ft InstanceName,@{L='CPU';E={($.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize |findstr java | Select-Object -First 1
    $java_used_cpu1 = $java_used_cpu_string -replace "^.+?\s+(.+)$",'$1'
    $java_used_cpu = $java_used_cpu1.Substring(0,$java_used_cpu1.Length-1)
    $num_java_used_cpu = [int]$java_used_cpu
    

    That should limit the results to 1, assume there was at least one instance running.


  2. Limitless Technology 39,351 Reputation points
    2021-11-01T08:47:12.773+00:00

    Hello YonglongYL44Wang

    I would recommend the next article regarding the calling of substring in variables: https://social.technet.microsoft.com/Forums/windowsserver/en-US/9363666b-cb2b-4cf7-9a96-290030ec8df0/problems-using-substring-method?forum=winserverpowershell


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments