Improved close-program.ps1

This commit is contained in:
Markus 2021-02-13 16:36:47 +01:00
parent 89c6ff871b
commit cd189f8b78
7 changed files with 25 additions and 17 deletions

View File

@ -6,4 +6,4 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
./close-program "calc" "Calculator"
./close-program "Calculator" "Calculator" "calc"

View File

@ -6,4 +6,4 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
./close-program "chrome" "Google Chrome"
./close-program "Google Chrome" "chrome"

View File

@ -6,4 +6,4 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
./close-process "msedge" "Microsoft Edge"
./close-program "Microsoft Edge" "msedge"

View File

@ -6,4 +6,4 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
./close-process "explorer" "File Explorer"
./close-program "File Explorer" "explorer"

View File

@ -1,12 +1,12 @@
#!/bin/powershell
<#
.SYNTAX ./close-program.ps1 [<program-name>] [<full-program-name>]
.SYNTAX ./close-program.ps1 [<full-program-name>][<program-name>] [<program-alias-name>]
.DESCRIPTION closes the processes of the given program gracefully
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param([string]$ProgramName = "", [string]$FullProgramName = "")
param($FullProgramName = "", $ProgramName = "", $ProgramAliasName = "")
try {
if ($ProgramName -eq "") {
@ -18,17 +18,25 @@ try {
}
$Processes = get-process -name $ProgramName -errorAction 'silentlycontinue'
foreach ($Process in $Processes) {
$_.CloseMainWindow() | Out-Null
}
start-sleep -milliseconds 100
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
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 {
$ProcessCount = $Processes.Count
if ($ProcessCount -eq 0) {
throw "$FullProgramName is not started yet"
$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'
}
write-host -foregroundColor green "Done - $FullProgramName with $ProcessCount process(es) has been closed."
write-host -foregroundColor green "Done - $FullProgramName has been closed ($($Processes.Count) proc)."
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -6,4 +6,4 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
./close-program "thunderbird" "Mozilla Thunderbird"
./close-program "Mozilla Thunderbird" "thunderbird"

View File

@ -6,4 +6,4 @@
.NOTES Author: Markus Fleschutz / License: CC0
#>
./close-program "WindowsTerminal" "Windows Terminal"
./close-program "Windows Terminal" "WindowsTerminal"