Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell v2 - how to get process ID

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?

like image 800
IGGt Avatar asked Sep 05 '14 13:09

IGGt


1 Answers

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

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

like image 53
Loïc MICHEL Avatar answered Oct 09 '22 01:10

Loïc MICHEL