28 lines
881 B
PowerShell
28 lines
881 B
PowerShell
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;
|
|
}
|
|
}
|
|
}
|
|
}
|