mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-02 19:09:13 +01:00
34 lines
1021 B
PowerShell
Executable File
34 lines
1021 B
PowerShell
Executable File
#!/snap/bin/powershell
|
|
<#
|
|
.SYNTAX ./close-program.ps1 [<program-name>]
|
|
.DESCRIPTION closes the given program gracefully
|
|
.LINK https://github.com/fleschutz/PowerShell
|
|
.NOTES Author: Markus Fleschutz / 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
|
|
}
|