powershell v2 - 如何获取进程 ID [英] powershell v2 - how to get process ID

查看:63
本文介绍了powershell v2 - 如何获取进程 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它运行自身的多个实例.例如

I have an Application, that runs multiple instances of itself. e.g

AppName.exe instance1
AppName.exe instance2
AppName.exe instance3

我正在尝试使用 Powershell v2 创建一个简单的脚本,该脚本给出了一组 AppNames 和实例,它循环遍历它们,检查它们是否正在运行,然后将它们关闭.

Using Powershell v2 I am trying to create a simple script that given an array of AppNames and instances, it loops through them, checks if they are running, and then shuts them down.

我认为最好的方法是检查每个实例,如果找到则捕获它的 processID,并将其传递给 stop-process cmdlet.

I figured the best way to do this would be check for each instance, if found capture it's processID, and pass that to the stop-process cmdlet.

但是,我不知道如何获取进程 ID.

BUT, I can't figure out how to get the process id.

到目前为止我有:

$appName = "AppName.exe"
$instance = "instance1"

$filter = "name like '%"+$appName+"%'"
$result = Get-WmiObject win32_process -Filter $filter

foreach($process in $result )
    {
        $desc = $process.Description
        $commArr = $process.CommandLine -split"( )" 
        $inst = $commArr[2] 
        $procID = "GET PROCESS ID HERE"

        if($inst -eq $instance)
            {
                Stop-Process $procID
            }
    }

谁能告诉我从哪里获取进程 ID?

Can anyone tell me where to get the process ID from please?

推荐答案

你可以使用 get-process cmdlet 而不是使用 wmi :

you can use the get-process cmdlet instead of using wmi :

$procid=get-process appname |select -expand id

这篇关于powershell v2 - 如何获取进程 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆