Last active
March 12, 2018 23:47
-
-
Save Jaykul/1f368143b7fe7e620f0c to your computer and use it in GitHub Desktop.
ChildProcess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Stop-ChildProcess { | |
[CmdletBinding(SupportsShouldProcess=$true)] | |
param( | |
[Parameter(ValueFromPipelineByPropertyName=$true, ParameterSetName="Name")] | |
[String[]]$Name, | |
[Parameter(ValueFromPipelineByPropertyName=$true, ParameterSetName="ID")] | |
[Alias("ProcessID")] | |
[Int[]]$ID, | |
[Switch]$IncludeParent | |
) | |
process { | |
$null = $PSBoundParameters.Remove("IncludeParent") | |
Get-ChildProcess @PSBoundParameters -Passthru:$IncludeParent | Stop-Process | |
} | |
} | |
function Get-ChildProcess { | |
[CmdletBinding(SupportsShouldProcess=$true, DefaultParameterSetName="ID")] | |
param( | |
[Parameter(ValueFromPipelineByPropertyName=$true, ParameterSetName="Name", Position=0)] | |
[String[]]$Name, | |
[Parameter(ValueFromPipelineByPropertyName=$true, ParameterSetName="ID", Position=0)] | |
[Alias("ProcessID")] | |
[Int[]]$ID, | |
[Switch]$Passthru | |
) | |
process { | |
$( | |
$null = $PSBoundParameters.Remove("Passthru") | |
foreach($ID in Get-Process @PSBoundParameters | % Id) { | |
Get-WmiObject Win32_Process -Filter "ParentProcessID=${ID}" | Select ProcessID | |
} | |
) | Get-ChildProcess -Passthru | |
if($Passthru) { | |
Get-Process @PSBoundParameters | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment