Files
management-scripts/Stop-Process.ps1
T

37 lines
1012 B
PowerShell
Raw Normal View History

2020-08-14 14:19:37 -04:00
Function Remediation-StopProcess
{
2020-05-22 14:35:46 -04:00
param(
[string]$Name,
[switch]$Force=$false
)
If ( $Name )
{
## Get all matching processes
$Processes = Get-Process | Where { $_.Name -ilike $Name }
## Get the total number of matching processes
$NumMatches = $Processes.Count
If ( $NumMatches -ge 1 )
{
## Forcefully close matching processes
If ( $Force )
{
Foreach ( $proc in $Processes ) { Stop-Process $proc -Force -ErrorAction SilentlyContinue }
Write-Output "Forcefully closed ${NumMatches} matching instance(s) of ${Name}"
}
## Gracefully close matching processes
Else
{
Foreach ( $proc in $Processes ) { $proc.CloseMainWindow() | Out-Null }
Write-Output "Gracefully closed ${NumMatches} matching instance(s) of ${Name}"
}
}
Else { Write-Output "No running processes match the name ${Name}" }
}
2020-08-14 14:19:37 -04:00
Else { Write-Output "No process name was specified!" }
}