Home > Blockchain >  Extract value of CPU usage from get-process command into variable, for further evaulation
Extract value of CPU usage from get-process command into variable, for further evaulation

Time:04-25

This command gives me proper output:

$Running = Get-process NODE | Select-Object CPU -ErrorAction SilentlyContinue

Output:

Output screenshot

I need to extract the number value from the returned object. Tried this via if, but .CPU returns nothing:

if($Running.CPU -gt 1) {Write-Output $Running}

CodePudding user response:

I don't know what NODE is, but I do have 2 instances of ttcalc running on my computer so I did a test with it

Get-process ttcalc | Select-Object CPU -ErrorAction SilentlyContinue | ForEach-Object {
    $Running = $_.CPU
    $Running
}

The output was:

2.484375
2.390625

Using GetType, it turns out that $Running is of type Double, so you should be able to do any calculation or test you want on it.

Get-process ttcalc | Select-Object CPU -ErrorAction SilentlyContinue | ForEach-Object {
    $Running = $_.CPU
    if($Running -gt 2.42) {
        Write-Host "ttcalc output was $Running"
    }
}

Output:

ttcalc output was 2.484375

As you can see, it caught one ttcalc, but not the other.

  • Related