我正在尝试显示no.of进程计数。条件是我有一个父进程ID和子进程,我能够检索并显示父进程和子进程,但我想显示与父进程有关的子进程的计数。
请找到我放入的脚本。
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
}
}
我用脚本输出的输出粘贴在
下面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
这里我试图只显示进程的计数。任何人都可以帮我如何显示此计数。
答案 0 :(得分:3)
它可能是一个额外的调用,但尝试Get-Process,显式查找有问题的processName并计算结果:
(Get-Process -Computer hostname.com -Name 'javaw').count
答案 1 :(得分:0)
将它们添加到数组中,并显示数组长度(或将它们分组):
$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
答案 2 :(得分:0)
感谢Maigi和4c74356b41。在你的帮助下我弄明白了。
$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 }
}
预期产出:
ADS services with Java processes
sm06388.dom1.e-ssi.net OpenLink_ADS_FENIX_PCT Running Auto
No.of Child Java process 11