ps/Modules/Alkami.PowerShell.Common/Public/Stop-ProcessIfFound.ps1

28 lines
881 B
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Stop-ProcessIfFound {
<#
.SYNOPSIS
Forcefully terminates a process if found
#>
param(
[Parameter(Position = 0, Mandatory = $true)]
[ValidateScript( {(!([String]::IsNullOrEmpty($_)))})]
[string]$processName
)
$logLead = (Get-LogLeadName);
$processes = Get-Process -Name $processName -ErrorAction SilentlyContinue;
Write-Verbose ("$logLead : Found {0} process(es) with name {1}" -f $processes.Count, $processName);
if ($processes.Count -gt 0) {
Write-Output ("$logLead : Stopping {0} instance(s) of process {1}" -f $processes.Count, $processName);
foreach ($process in ($processes | Where-Object {$null -ne $_.Id})) {
if ($null -ne (Get-Process -Id $process.Id -ErrorAction SilentlyContinue)) {
Stop-Process -InputObject $process -Force;
}
}
}
}