【问题标题】:How can I forcibly kill a process using powershell?如何使用 powershell 强行终止进程?
【发布时间】:2017-04-07 20:55:51
【问题描述】:

我想杀死一个进程:

$ ps Sourcetree

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   1311     116   136720     137804      57.45   1044   1 SourceTree

发给Stop-Process, aka kill:

$ ps Sourcetree | kill

运行没有错误。但是进程没有被杀死:

$ ps Sourcetree

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   1311     116   136720     137804      57.45   1044   1 SourceTree

如何使用 powershell 强制终止进程?

【问题讨论】:

标签: windows powershell process


【解决方案1】:

如果你这样做?

(get-process -Name SourceTree).Kill()

【讨论】:

  • 不会杀死它。根据上面接受的答案,我需要-force
【解决方案2】:

使用-Force

ps -force Sourcetree | kill

取自help Stop-Process -online

Stop-Process [-Id] <Int32[]> [-PassThru] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

-Force

Stops the specified processes without prompting for confirmation.
By default, Stop-Process prompts for confirmation before stopping
any process that is not owned by the current user.

To find the owner of a process, use the Get-WmiObject cmdlet to
get a Win32_Process object that represents the process, and then
use the GetOwner method of the object.
Type: SwitchParameter
Parameter Sets: (All)
Aliases: 

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

Bonus nachos,Get-Command -ParameterName *force 将显示所有包含 force 参数的 Cmdlet。

【讨论】:

  • 我认为这不等同于 taskkill /f 或 unix 中的“kill -9”。似乎没有直接的方法。
【解决方案3】:

为了更好地理解 cmdlet 以及它们的作用,您可以随时使用Get-Help cmdlet 并查看它的作用\支持。

Get-Help Stop-Process

或者只是将 cmdlet 输入到搜索引擎中,这很可能会将您引导至官方帮助文章。

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/stop-process

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2018-03-30
    • 2013-04-24
    • 2014-12-06
    • 1970-01-01
    • 2019-12-08
    相关资源
    最近更新 更多