powershell - 检测以相同名称运行的进程数

标签 powershell process instances

关于如何编写一个返回正在运行的进程的实例数的函数有什么想法吗?

也许是这样的?

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/

相关文章:

powershell - Powershell检查点作为作业或工作流

powershell - 如何在管道中加入数组

父进程退出后,子进程无法从终端读取

bash - 如何将多个命令通过管道传输到 shell 中的单个命令? (嘘,庆典,...)

java - 从其他类控制 JComponent

android - Android Java 应用程序中的实例计数违规

ruby - Powershell 和 Ruby 中不同的 SHA1 哈希结果

powershell - 如果由于 URL 无效/过期而导致 invoke-webrequest 失败,如何回显,否则继续在 Powershell CLI 中下载?

Linux:修改/proc的内容

java - 有多少实例?