powershell v2 - how to get process ID

萝らか妹 提交于 2019-12-02 02:58:28

问题


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

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

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.

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.

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

So far I have:

$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
            }
    }

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


回答1:


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

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




回答2:


$procid=(get-process appname).id


来源:https://stackoverflow.com/questions/25686547/powershell-v2-how-to-get-process-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!