2021-02-09 19:30:45 +01:00
|
|
|
#!/bin/powershell
|
2020-12-29 15:14:21 +01:00
|
|
|
<#
|
2021-02-13 16:36:47 +01:00
|
|
|
.SYNTAX ./close-program.ps1 [<full-program-name>][<program-name>] [<program-alias-name>]
|
2020-12-29 16:28:03 +01:00
|
|
|
.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
|
|
|
|
2021-02-13 16:36:47 +01:00
|
|
|
param($FullProgramName = "", $ProgramName = "", $ProgramAliasName = "")
|
2020-12-27 11:02:20 +01:00
|
|
|
|
2021-02-18 20:17:55 +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-28 10:23:17 +01:00
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
try {
|
2020-12-29 16:28:03 +01:00
|
|
|
$Processes = get-process -name $ProgramName -errorAction 'silentlycontinue'
|
2021-02-13 16:36:47 +01:00
|
|
|
if ($Processes.Count -ne 0) {
|
|
|
|
foreach ($Process in $Processes) {
|
|
|
|
$Process.CloseMainWindow() | Out-Null
|
|
|
|
}
|
|
|
|
start-sleep -milliseconds 100
|
|
|
|
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
|
|
|
|
} else {
|
2020-12-28 10:23:17 +01:00
|
|
|
|
2021-02-13 16:36:47 +01:00
|
|
|
$Processes = get-process -name $ProgramAliasName -errorAction 'silentlycontinue'
|
|
|
|
if ($Processes.Count -eq 0) {
|
|
|
|
throw "$FullProgramName is not started yet"
|
|
|
|
}
|
|
|
|
foreach ($Process in $Processes) {
|
|
|
|
$_.CloseMainWindow() | Out-Null
|
|
|
|
}
|
|
|
|
start-sleep -milliseconds 100
|
|
|
|
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
|
2020-12-27 11:02:20 +01:00
|
|
|
}
|
2021-02-13 16:36:47 +01:00
|
|
|
write-host -foregroundColor green "Done - $FullProgramName has been closed ($($Processes.Count) proc)."
|
2020-12-28 09:40:26 +01:00
|
|
|
exit 0
|
2020-12-27 11:02:20 +01:00
|
|
|
} catch {
|
2021-02-16 10:03:20 +01:00
|
|
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2020-12-27 11:02:20 +01:00
|
|
|
exit 1
|
|
|
|
}
|