none
Unable to get duplicate instances of svchost and their ID

    Question

  • Hello ,

    I was trying to get services used svchost processes if they exist in top 5 processes  sorted according to working set.

    I can get  PID and service name of single instance of svchost if it exist in top 5 processes . If there is another svchost listed in top 5 its not showing.

    Below is the code.

    $Array1 = @(get-process |sort ws -Descending |select -First 10 |select processname,ID )
    $Array2 = @(Get-WmiObject win32_service -Filter "Pathname like '%svchost%'"| where{ $_.state -eq'running' } | select  ProcessID ,name )
    foreach ($elem in $Array2) { if ($Array1 -contains $elem)
    {$elem.ProcessID  } }
    Write-Output Name = $elem.name
    Write-Output ProcessID = $elem.ProcessID
    Name
    =
    wudfsvc
    ProcessID
    =
    1236

    Thursday, July 12, 2018 6:45 PM

All replies

  • You could choose a somewhat clearer approach .... something like this

    $Array1 = @( Get-Process | Sort-Object -Property WS -Descending | Select-Object -First 10 | Select-Object -Property ProcessName,ID ) $Array2 = @( Get-WmiObject -Class win32_service -Filter "Pathname like '%svchost%'" | Where-Object { $_.state -eq 'running' } | Select-Object -Property @{Name='ID';Expression={$_.ProcessID}}, Name ) Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2 -Property ID -IncludeEqual -PassThru

    ... and please format your code as code. Thanks


    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH_666 Thursday, July 12, 2018 11:12 PM
    Thursday, July 12, 2018 11:10 PM