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

发布于 2024-11-19 08:09:36 字数 526 浏览 8 评论 0原文

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

也许是这样的?

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
}

Any ideas of how to write a function that returns the number of instances of a process is running?

Perhaps something like this?

function numInstances([string]$process)
{
    $i = 0
    while(<we can get a new process with name $process>)
    {
        $i++
    }

    return $i
}

Edit: 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疯到世界奔溃 2024-11-26 08:09:36
function numInstances([string]$process)
{
    @(get-process -ea silentlycontinue $process).count
}

编辑:添加了静默继续和数组转换以与零个和一个进程一起使用。

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.

格子衫的從容 2024-11-26 08:09:36

这对我有用:

function numInstances([string]$process)
{
    @(Get-Process $process -ErrorAction 0).Count
}

# 0
numInstances notepad

# 1
Start-Process notepad
numInstances notepad

# many
Start-Process notepad
numInstances notepad

输出:

0
1
2

虽然很简单,但此解决方案中有两个要点:1)使用 -ErrorAction 0 (0 与 SilentlyContinue 相同),所以当没有指定的流程时它​​可以很好地工作; 2) 使用数组运算符@(),以便它在只有单个流程实例时起作用。

This works for me:

function numInstances([string]$process)
{
    @(Get-Process $process -ErrorAction 0).Count
}

# 0
numInstances notepad

# 1
Start-Process notepad
numInstances notepad

# many
Start-Process notepad
numInstances notepad

Output:

0
1
2

Though it is simple there are two important points in this solution: 1) use -ErrorAction 0 (0 is the same as SilentlyContinue), so that it works well when there are no specified processes; 2) use the array operator @() so that it works when there is a single process instance.

笛声青案梦长安 2024-11-26 08:09:36

使用内置的 cmdlet 组对象要容易得多:

 get-process | Group-Object -Property ProcessName

Its much easier to use the built-in cmdlet group-object:

 get-process | Group-Object -Property ProcessName
烧了回忆取暖 2024-11-26 08:09:36

有一个很好的单行:(ps).count

There is a nice one-liner : (ps).count

油饼 2024-11-26 08:09:36

(Get-Process |Where-Object {$_.Name -eq 'Chrome'}).count

这将返回正在运行的同名进程的数量。您可以添加过滤器以进一步格式化数据。

(Get-Process | Where-Object {$_.Name -eq 'Chrome'}).count

This will return you the number of processes with same name running. you can add filters to further format your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文