简体   繁体   English

尝试使用PowerShell获取进程数

[英]Trying to get the number of process count using PowerShell

I am trying to get no.of process count to be displayed. 我想要显示no.of进程计数。 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. 条件是我有一个父进程ID和子进程,我能够检索并显示父进程和子进程,但我想显示与父进程有关的子进程的计数。

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. 任何人都可以帮我如何显示此计数。

它可能是一个额外的调用,但尝试Get-Process,显式查找有问题的processName并计算结果:

(Get-Process -Computer hostname.com -Name 'javaw').count

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

Thanks Maigi and 4c74356b41. 谢谢Maigi和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM