1

I am trying to get no.of process count to be displayed. The condition is I have a parent process ID and child process I was able to retrieve and display the parent and child process but I want to display the count of child process pertaining to the parent process.

Please find the piece of script that I have put it in.

Write-Host "ADS services with Java processes"
Invoke-Command -Computer SM06388.dom1.e-ssi.net -ScriptBlock {
    Get-WmiObject -Class Win32_Service -Filter "name='OpenLink_ADS_Fenix_PCT'"
} | ForEach-Object {
  if ($_.State -eq "running") {
    Write-Host $_.PSComputerName $_.Name $_.State $_.StartMode -  ForegroundColor green
    Get-WmiObject -Class Win32_Process -Filter "Name='java_svc_wrapper.exe'" | ForEach-Object {
      Get-WmiObject Win32_Process -Filter  "ParentProcessId=$($_.ProcessId)"
    } | Format-Table ProcessName,ProcessId,Handle,ParentProcessId -Auto
  } else {
    Write-Host $_.PSComputerName $_.Name $_.State $_.StartMode -ForegroundColor red
  }
}

The output that I get with the script is pasted below

ADS services with Java processes
sm06388.dom1.e-ssi.net OpenLink_ADS_FENIX_PCT Running Auto

ProcessName ProcessId Handle ParentProcessId
----------- --------- ------ ---------------
java.exe        12164 12164             5520
java.exe         9392 9392              5520
java.exe        12892 12892             5520
java.exe        10396 10396             5520
java.exe         9868 9868              5520
java.exe        11584 11584             5520
java.exe        14760 14760             5520
java.exe         9740 9740              5520
java.exe        12232 12232             5520
java.exe        16432 16432             5520
java.exe        15688 15688             5520

Here I am trying to just display the count of the process. Can anyone help me how to get this count displayed.

3 Answers 3

4

It might be an extra call but try Get-Process, explicitly look for the processName in question and count the result:

(Get-Process -Computer hostname.com -Name 'javaw').count
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Maigi. But are you sure Get-Process command can be used for a remote machine? Because I dont think so
Since it's a valid parameter in the documentation (referring to MSDN) I'd say yes. I verified it aswell before posting
0

Add them to array, and display array length (or group them):

$results = @()

write-host "ADS services with Java processes"
invoke-command -computer SM06388.dom1.e-ssi.net -scriptblock {
    get-wmiobject -Class win32_service -Filter "name='OpenLink_ADS_Fenix_PCT'"
} | foreach-object {
    if ($_.State -eq "running") {
        write-host $_.PSComputerName  $_.Name $_.State $_.StartMode -  ForegroundColor green
        Get-WmiObject -Class Win32_Process -Filter "Name='java_svc_wrapper.exe'"| 
        ForEach-Object {
            $results += Get-WmiObject Win32_Process -Filter  "ParentProcessId=$($_.ProcessId)"
        }
    }
    else {
        write-host $_.PSComputerName $_.Name $_.State $_.StartMode -ForegroundColor red }
    }

    $results

1 Comment

Thanks but without the format table the result looks with all the details which are unnecessary. And I am pretty new to programming can you please help me here how to display the array length and group them. I just tried executing the above piece of code that you have pasted. But I got all unwanted information displayed.
0

Thanks Maigi and 4c74356b41. with your help I have got it figured out.

$results = @()

write-host "ADS services with Java processes"
invoke-command -computer SM06388.dom1.e-ssi.net -scriptblock {get-wmiobject   -Class win32_service -Filter "name='OpenLink_ADS_Fenix_PCT'"} | foreach-object {
if ($_.State -eq "running") 
{write-host $_.PSComputerName  $_.Name $_.State $_.StartMode -   ForegroundColor green
Get-WmiObject -Class Win32_Process -Filter "Name='java_svc_wrapper.exe'"|     ForEach-Object{
$results += (Get-WmiObject Win32_Process -Filter  "ParentProcessId=$($_.ProcessId)").count} 
Write-host "No.of Child Java process $results" -ForegroundColor green
}
else 
{write-host $_.PSComputerName $_.Name $_.State $_.StartMode -ForegroundColor  red }
}

Expected output:

ADS services with Java processes
sm06388.dom1.e-ssi.net OpenLink_ADS_FENIX_PCT Running Auto
No.of Child Java process 11

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.