From 4946b67d1fa8b9c8201c9906e5259e3096f9c2d0 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Mon, 28 Dec 2020 08:40:26 +0000 Subject: [PATCH] Added close-chrome.ps1 and close-thunderbird.ps1 --- README.md | 2 ++ Scripts/close-chrome.ps1 | 10 ++++++++++ Scripts/close-program.ps1 | 17 +++++++++-------- Scripts/close-thunderbird.ps1 | 10 ++++++++++ Scripts/close-windows-terminal.ps1 | 11 +++-------- 5 files changed, 34 insertions(+), 16 deletions(-) create mode 100755 Scripts/close-chrome.ps1 create mode 100755 Scripts/close-thunderbird.ps1 diff --git a/README.md b/README.md index 267df190..c18a4c7e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol * [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity * [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity * [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories +* [close-chrome.ps1](Scripts/close-chrome.ps1) - closes Google Chrome gracefully * [close-program.ps1](Scripts/close-program.ps1) - closes the given program gracefully +* [close-thunderbird.ps1](Scripts/close-thunderbird.ps1) - closes Mozilla Thunderbird gracefully * [close-windows-terminal.ps1](Scripts/close-windows-terminal.ps1) - closes Windows Terminal gracefully * [configure-git.ps1](Scripts/configure-git.ps1) - sets up the Git configuration * [csv-to-text.ps1](Scripts/csv-to-text.ps1) - converts the given CSV file into a text list diff --git a/Scripts/close-chrome.ps1 b/Scripts/close-chrome.ps1 new file mode 100755 index 00000000..93717cff --- /dev/null +++ b/Scripts/close-chrome.ps1 @@ -0,0 +1,10 @@ +#!/snap/bin/powershell + +# Syntax: ./close-chrome.ps1 +# Description: closes Google Chrome gracefully +# Author: Markus Fleschutz +# Source: github.com/fleschutz/PowerShell +# License: CC0 + +$ExitCode = close-program.ps1 "chrome" +exit $ExitCode diff --git a/Scripts/close-program.ps1 b/Scripts/close-program.ps1 index 4a37aa6d..9f51e4f6 100755 --- a/Scripts/close-program.ps1 +++ b/Scripts/close-program.ps1 @@ -14,15 +14,16 @@ try { $ProgramName = read-host "Enter program to close" } $List = Get-Process -name $ProgramName -erroraction 'silentlycontinue' - $NumProc = 0 - $List | Foreach-Object { $NumProc++; $_.CloseMainWindow() | Out-Null } | stop-process -force - if ($NumProc -eq 0) { - write-error "ERROR: No processes for program '$ProgramName' found" - exit 1 - } else { - write-output "Done - $NumProc processes stopped." - exit 0 + $NumProcKilled = 0 + $List | Foreach-Object { + $_.CloseMainWindow() | Out-Null + $NumProcKilled++ + } | stop-process -force + if ($NumProcKilled -eq 0) { + throw "program '$ProgramName' is not started yet" } + write-output "OK - program '$ProgramName' has been closed ($NumProcKilled processes)." + exit 0 } catch { write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" exit 1 diff --git a/Scripts/close-thunderbird.ps1 b/Scripts/close-thunderbird.ps1 new file mode 100755 index 00000000..f2dc5e01 --- /dev/null +++ b/Scripts/close-thunderbird.ps1 @@ -0,0 +1,10 @@ +#!/snap/bin/powershell + +# Syntax: ./close-thunderbird.ps1 +# Description: closes Mozilla Thunderbird gracefully +# Author: Markus Fleschutz +# Source: github.com/fleschutz/PowerShell +# License: CC0 + +$ExitCode = close-program.ps1 "thunderbird" +exit $ExitCode diff --git a/Scripts/close-windows-terminal.ps1 b/Scripts/close-windows-terminal.ps1 index 8466cf00..7a7ba3dd 100755 --- a/Scripts/close-windows-terminal.ps1 +++ b/Scripts/close-windows-terminal.ps1 @@ -1,15 +1,10 @@ #!/snap/bin/powershell # Syntax: ./close-windows-terminal.ps1 -# Description: closes the Windows Terminal program gracefully +# Description: closes Windows Terminal gracefully # Author: Markus Fleschutz # Source: github.com/fleschutz/PowerShell # License: CC0 -close-program.ps1 "WindowsTerminal" -#try { -# exit 0 -#} catch { -# write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" -# exit 1 -#} +$ExitCode = close-program.ps1 "WindowsTerminal" +exit $ExitCode