在powershell中停止然后启动一个进程 [英] Stop and then start a process in powershell

查看:62
本文介绍了在powershell中停止然后启动一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止/终止某个进程,然后在完成我必须做的事情后重新启动它.

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

这是我已经拥有的.

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

但是 Start 需要一些我认为可以从该过程中获得的参数.但我不确定我知道该给它什么.

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.

推荐答案

$processes[$in-1].Start() 将不起作用.您需要捕获要杀死的进程信息并再次启动同一个应用程序.您可以使用 Win32_Process WMI 类获取进程二进制文件和命令行信息.

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.

例如

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()
}

在上面的示例中,我使用 WMI 获取所选进程的命令行信息.如果这是一个带有一些打开文本文件的记事本进程,该进程的命令行看起来像 "C:\WINDOWS\system32\NOTEPAD.EXE" C:\Users\ravikanth_chaganti\Desktop\debug.log

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.

希望这能给你一个想法.其他 PowerShell 专家可能有不同的 &有效的方法.这只是一个快速的技巧.

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

这篇关于在powershell中停止然后启动一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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