mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-24 00:43:35 +01:00
34 lines
1007 B
PowerShell
Executable File
34 lines
1007 B
PowerShell
Executable File
#!/snap/bin/powershell
|
|
|
|
# Syntax: ./close-program.ps1 [<program-name>]
|
|
# Description: closes the given program gracefully
|
|
# Author: Markus Fleschutz
|
|
# Source: github.com/fleschutz/PowerShell
|
|
# License: CC0
|
|
|
|
param([string]$ProgramName)
|
|
|
|
try {
|
|
if ($ProgramName -eq "" ) {
|
|
Get-Process | Where-Object {$_.mainWindowTitle} | Format-Table Id, Name, mainWindowtitle -AutoSize
|
|
$ProgramName = read-host "Enter program to close"
|
|
}
|
|
|
|
$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'
|
|
|
|
$ProcessCount = $Processes.Count
|
|
if ($ProcessCount -eq 0) {
|
|
throw "program '$ProgramName' is not started yet"
|
|
}
|
|
write-output "OK - program '$ProgramName' with $ProcessCount process(es) has been closed."
|
|
exit 0
|
|
} catch {
|
|
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|