Public/Utils/Stop-ProcessForcefully.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Stop-ProcessForcefully {

    <#
    .SYNOPSIS
    Kills process forcefully along with its children.

    .EXAMPLE
    Stop-ProcessForcefully -Process $process
    #>


    [CmdletBinding()]
    [OutputType([void])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWMICmdlet', '')]
    param(
        # Process object.
        [Parameter(Mandatory = $true)]
        [object]
        $Process,

        # Time to wait for process before killing it.
        [Parameter(Mandatory = $true)]
        [int]
        $KillTimeoutInSeconds
    )

    $childProcesses = Get-WmiObject -Class Win32_Process -Filter "ParentProcessID=$($Process.Id)" | Select-Object -ExpandProperty ProcessID

    try {
        if ($childProcesses) {
            Write-Log -Info "Killing child processes: $childProcesses"
            Stop-Process -Id $childProcesses -Force
        }
        else {
            Write-Log -Info "No child processes for pid $($Process.Id)"
        }
        Write-Log -Info "Killing process $($Process.Id)"
        $Process.Kill()
    }
    catch {
        Write-Log -Warn "Kill method thrown exception: $_ - waiting for exit."
    }
    if (!$Process.WaitForExit($KillTimeoutInSeconds * 1000)) {
        throw "Cannot kill process (pid $($Process.Id)) - still running after $($KillTimeoutInSeconds * 1000 * 2) s"
    }
    Write-Log -Info "Process $($Process.Id) killed along with its children."
}