关于如何编写一个返回正在运行的进程的实例数的函数有什么想法吗?
也许是这样的?
function numInstances([string]$process)
{
$i = 0
while(<we can get a new process with name $process>)
{
$i++
}
return $i
}
编辑:开始编写一个函数...它适用于单个实例,但如果运行多个实例,它就会进入无限循环:
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
}
编辑:添加了静默继续和数组转换以与零个和一个进程一起使用。
关于powershell - 检测以相同名称运行的进程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6623433/