0

I have a Powershell command (which checks % CPU usage of a process) that returns me 2 values (Instance name + CPU%). I want to return just ONE value (CPU%). What should I add to this script to do that? Example below:

$Processname = "slack"
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$Samples = (Get-Counter “\Process($Processname)\% Processor Time”).CounterSamples
$Samples | Select `
InstanceName,
@{Name=”CPU %”;Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}

The return value is:

InstanceName CPU %
------------ -----
slack            0

I want only the 0.

2 Answers 2

1

Try this-

$Processname = "slack"
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$Samples = (Get-Counter “\Process($Processname)\% Processor Time”).CounterSamples
($Samples | Select InstanceName, @{Name=”CPU”;Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}).CPU
Sign up to request clarification or add additional context in comments.

Comments

1

I would first expand the CookedValue property (using ExpandProperty, the general way to just return one value from a PowerShell object) and then make the calculation:

[Decimal]::Round((($Samples | Select -Expand CookedValue) / $CpuCores), 2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.