Files
management-scripts/Control-Process.ps1
T

33 lines
1.2 KiB
PowerShell
Raw Normal View History

2020-11-06 11:24:16 -05:00
## Stop a process
Function End-Process
{
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Name,
[Parameter(Mandatory=$false)][switch]$Match=$false,
[Parameter(Mandatory=$false)][switch]$Force
)
ForEach ( $item in $Name ) {
$process = Get-Process -Name $item -ErrorAction SilentlyContinue
If ( !($process) ) {
LogMsg "Cannot find the process '${item}'"
If ( $Match ) {
LogMsg "Attempting to match '${item}' to all running process names"
ForEach ( $proc in ((Get-Process | Where { $_.Name -match $item }).Name) ) {
If ( $Force ) { End-Process -Name $proc -Force } Else { End-Process -Name $proc }
}
}
}
Else {
$name = $process.Name
If ( $Force ) {
LogMsg "Forcefully stopping '${name}' process"
Stop-Process $process -Force -ErrorAction SilentlyContinue
}
Else {
LogMsg "Attempting to gracefully close '${name}' process"
$process.CloseMainWindow() | Out-Null
}
}
}
}