Tom
Tom

Reputation: 29

Powershell ForEach issue

Could someone explain whats wrong with this?

Thanks Tom

$ProcessData = (Get-Counter '\Process(svchost*)\% Processor Time').Countersamples | Sort cookedvalue -Desc | Select-Object -First 1
ForEach($NamedProcess in $ProcessData)
{
echo $NamedProcess.ProcessId
}

Upvotes: 0

Views: 75

Answers (2)

Mudit Bahedia
Mudit Bahedia

Reputation: 246

If you really want to know PID for InstanceName, you can use below code:

$ProcessData = (Get-Counter '\Process(svchost*)\% Processor Time').Countersamples | Sort cookedvalue -Desc | Select-Object -First 1
Get-Process -Name $ProcessData.InstanceName | Select ID

Upvotes: 1

TobyU
TobyU

Reputation: 3908

Based on the comments I've got rid of the unnecessary ForEach and changed ProcessId to InstanceName since there is no value for ProcessId available with the Get-Counter cmdlet.

$ProcessData = (Get-Counter '\Process(svchost*)\% Processor Time').Countersamples | Sort cookedvalue -Desc | Select-Object -First 1
$ProcessData.InstanceName

Upvotes: 1

Related Questions