2020-12-27 11:02:20 +01:00
|
|
|
#!/snap/bin/powershell
|
2020-12-29 15:14:21 +01:00
|
|
|
<#
|
2020-12-29 16:28:03 +01:00
|
|
|
.SYNTAX ./close-program.ps1 [<program-name>] [<full-program-name>]
|
|
|
|
.DESCRIPTION closes the processes of the given program gracefully
|
2020-12-29 15:14:21 +01:00
|
|
|
.LINK https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES Author: Markus Fleschutz / License: CC0
|
|
|
|
#>
|
2020-12-27 11:02:20 +01:00
|
|
|
|
2020-12-29 16:28:03 +01:00
|
|
|
param([string]$ProgramName = "", [string]$FullProgramName = "")
|
2020-12-27 11:02:20 +01:00
|
|
|
|
|
|
|
try {
|
2020-12-29 16:28:03 +01:00
|
|
|
if ($ProgramName -eq "") {
|
|
|
|
get-process | where-object {$_.mainWindowTitle} | format-table Id, Name, mainWindowtitle -AutoSize
|
|
|
|
$ProgramName = read-host "Enter program name"
|
|
|
|
}
|
|
|
|
if ($FullProgramName -eq "") {
|
|
|
|
$FullProgramName = $ProgramName
|
2020-12-27 11:02:20 +01:00
|
|
|
}
|
2020-12-28 10:23:17 +01:00
|
|
|
|
2020-12-29 16:28:03 +01:00
|
|
|
$Processes = get-process -name $ProgramName -errorAction 'silentlycontinue'
|
2020-12-28 10:23:17 +01:00
|
|
|
foreach ($Process in $Processes) {
|
2020-12-28 09:40:26 +01:00
|
|
|
$_.CloseMainWindow() | Out-Null
|
2020-12-28 10:23:17 +01:00
|
|
|
}
|
|
|
|
start-sleep -milliseconds 100
|
2020-12-29 16:28:03 +01:00
|
|
|
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
|
2020-12-28 10:23:17 +01:00
|
|
|
|
|
|
|
$ProcessCount = $Processes.Count
|
|
|
|
if ($ProcessCount -eq 0) {
|
2020-12-29 16:28:03 +01:00
|
|
|
throw "$FullProgramName is not started yet"
|
2020-12-27 11:02:20 +01:00
|
|
|
}
|
2020-12-29 16:28:03 +01:00
|
|
|
write-output "OK - $FullProgramName with $ProcessCount process(es) has been closed."
|
2020-12-28 09:40:26 +01:00
|
|
|
exit 0
|
2020-12-27 11:02:20 +01:00
|
|
|
} catch {
|
|
|
|
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|