4

I would like to stop/kill a certain process and then start it again after I am done doing what I have to do.

This is what I already have.

Clear-host
$processes = Get-Process devenv 
$processes.Count
if($processes.Count -gt 1)
{
    $i = 0
    Write-host "There are multiple processes for devenv."
    foreach($process in $processes)
    {
        $i++
        $i.ToString() + '. ' + $process.MainWindowTitle
    }
    $in = Read-host "Give a number of the process to kill: "
    write-host
    write-host "killing and restarting: " + $processes[$in-1].MainWindowTitle
    $processes[$in-1].Kill()
    $processes[$in-1].WaitForExit()
    $processes[$in-1].Start()

}
else
{
    write-host "something else"
}

But the Start needs some parameter which I thought I could get from the process. But I'm not really sure I know what to give it.

chrissie1
  • 5,014
  • 3
  • 26
  • 26

1 Answers1

7

The $processes[$in-1].Start() will not work. You need to capture the processinfo you are killing and start the same app again. You can get the process binary and commandline information using Win32_Process WMI class.

For example,

Clear-host
$processes = Get-Process notepad 
$processes.Count
if($processes.Count -gt 1)
{
    $i = 0
    Write-host "There are multiple processes for notepad."
    foreach($process in $processes)
    {
        $i++
        $i.ToString() + '. ' + $process.MainWindowTitle
    }
    $in = Read-host "Give a number of the process to kill: "
    write-host
    write-host "killing and restarting: " + $processes[$in-1].MainWindowTitle

    #Get the process details
    $procID = $processes[$in-1].Id
    $cmdline = (Get-WMIObject Win32_Process -Filter "Handle=$procID").CommandLine
    $processes[$in-1].Kill()
    $processes[$in-1].WaitForExit()
}

In the above example, I am using WMI to get the commandline information for a process selected. If that were a notepad process with some open text file, the commandline for that process would look like "C:\WINDOWS\system32\NOTEPAD.EXE" C:\Users\ravikanth_chaganti\Desktop\debug.log

Now, all you need to do is: Invoke that commandline somehow (this part is not there in example I wrote). A very blunt way to do that is:

Start-Process -FilePath $cmdline.Split(' ')[0] -ArgumentList $cmdline.Split(' ')[1]

But, in your case, there may not be any argument list.

Hope this gives you an idea. Other PowerShell experts may have a different & efficient approach. This is just a quick hack.

ravikanth
  • 24,922
  • 4
  • 60
  • 60
  • I have been trying this but win7/64x seems to not know the win32_process class whatever I tell it. – chrissie1 Feb 24 '11 at 09:31
  • Hmm..I wrote/tried this example on a Win7 x64 system. What exactly did you try? – ravikanth Feb 24 '11 at 10:53
  • I tried exatcly what you dscribed and get an invalid class exception, when I do get-wmiobject -list I don't see win32_process in it, I run powershell ISE as administrator and that doesn't help either. grr. – chrissie1 Feb 24 '11 at 11:03
  • Wow, what do you see when run this: Get-WMIObject -Namespace root\cimv2 -Class WIn32_Process – ravikanth Feb 24 '11 at 11:08
  • Get-WmiObject : Invalid class At line:1 char:14 + Get-WMIObject <<<< -Namespace root\cimv2 -Class WIn32_Process + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand – chrissie1 Feb 24 '11 at 11:15
  • looks like your WMI repo is corrupt. not sure how you can go about repairing it. – ravikanth Feb 24 '11 at 11:25
  • I think this should do it. http://stackoverflow.com/questions/3678390/wmi-namespace-root-cimv2-not-available-on-win2k/3685006#3685006 will try soonish. – chrissie1 Feb 24 '11 at 11:46
  • Had to recompile the mof and mfl file after rebuilding the repo solution found here http://bit.ly/hU2Pnm – chrissie1 Feb 24 '11 at 13:31