检测以相同名称运行的进程数 [英] Detect number of processes running with the same name
本文介绍了检测以相同名称运行的进程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于如何编写一个返回正在运行的进程实例数的函数的想法吗?
Any ideas of how to write a function that returns the number of instances of a process is running?
也许是这样吗?
function numInstances([string]$process)
{
$i = 0
while(<we can get a new process with name $process>)
{
$i++
}
return $i
}
开始编写函数...它适用于单个实例,但如果有多个实例在运行,它将陷入无限循环:
Started to write a function... It works for a single instance but it goes into an infinite loop if more than one instance is running:
function numInstances([string]$process)
{
$i = 0
$ids = @()
while(((get-process $process) | where {$ids -notcontains $_.ID}) -ne $null)
{
$ids += (get-process $process).ID
$i++
}
return $i
}
推荐答案
function numInstances([string]$process)
{
@(get-process -ea silentlycontinue $process).count
}
编辑:添加了静默继续和数组强制转换,以使用零个和一个进程.
EDIT: added the silently continue and array cast to work with zero and one processes.
这篇关于检测以相同名称运行的进程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文