From f771cf744195c75b9fee4612deb555242f4e5033 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Wed, 9 Dec 2020 09:30:55 +0000 Subject: [PATCH] Improved the error message --- Scripts/MD5.ps1 | 6 ++++-- Scripts/SHA1.ps1 | 6 ++++-- Scripts/SHA256.ps1 | 6 ++++-- Scripts/alert.ps1 | 6 ++++-- Scripts/clone-repos.ps1 | 19 ++++++++++++------- Scripts/configure-git.ps1 | 6 ++++-- Scripts/download.ps1 | 6 ++++-- Scripts/inspect-exe.ps1 | 6 ++++-- Scripts/latlong.ps1 | 6 ++++-- Scripts/list-cmdlets.ps1 | 6 ++++-- Scripts/list-empty-dirs.ps1 | 6 ++++-- Scripts/list-modules.ps1 | 6 ++++-- Scripts/list-processes.ps1 | 6 ++++-- Scripts/list-unused-files.ps1 | 6 ++++-- Scripts/make-install.ps1 | 6 ++++-- Scripts/moon.ps1 | 6 ++++-- Scripts/news.ps1 | 6 ++++-- Scripts/password.ps1 | 6 ++++-- Scripts/passwords.ps1 | 6 ++++-- Scripts/poweroff.ps1 | 6 ++++-- Scripts/reboot.ps1 | 6 ++++-- Scripts/send-email.ps1 | 6 ++++-- Scripts/send-udp.ps1 | 6 ++++-- Scripts/speak-file.ps1 | 6 ++++-- Scripts/speak-text.ps1 | 6 ++++-- Scripts/switch-shelly1.ps1 | 6 ++++-- Scripts/test.ps1 | 6 ++++-- Scripts/train-dns-cache.ps1 | 6 ++++-- Scripts/translate-text.ps1 | 6 ++++-- Scripts/txt2wav.ps1 | 6 ++++-- Scripts/wakeup.ps1 | 6 ++++-- Scripts/weather-alert.ps1 | 6 ++++-- Scripts/weather-report.ps1 | 6 ++++-- Scripts/weather-worldwide.ps1 | 6 ++++-- Scripts/weather.ps1 | 6 ++++-- Scripts/zip-dir.ps1 | 6 ++++-- 36 files changed, 152 insertions(+), 77 deletions(-) diff --git a/Scripts/MD5.ps1 b/Scripts/MD5.ps1 index e1df824a..fe77a317 100755 --- a/Scripts/MD5.ps1 +++ b/Scripts/MD5.ps1 @@ -15,5 +15,7 @@ try { $Result = get-filehash $File -algorithm MD5 write-host "✔️ MD5 hash is" $Result.Hash exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/SHA1.ps1 b/Scripts/SHA1.ps1 index 03ba9137..f1722e97 100755 --- a/Scripts/SHA1.ps1 +++ b/Scripts/SHA1.ps1 @@ -15,5 +15,7 @@ try { $Result = get-filehash $File -algorithm SHA1 write-host "✔️ SHA1 hash is" $Result.Hash exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/SHA256.ps1 b/Scripts/SHA256.ps1 index 7182916a..3ea683ed 100755 --- a/Scripts/SHA256.ps1 +++ b/Scripts/SHA256.ps1 @@ -15,5 +15,7 @@ try { $Result = get-filehash $File -algorithm SHA256 write-host "✔️ SHA256 hash is" $Result.Hash exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/alert.ps1 b/Scripts/alert.ps1 index 383c9f9d..7c82c634 100755 --- a/Scripts/alert.ps1 +++ b/Scripts/alert.ps1 @@ -17,5 +17,7 @@ try { curl --header "Access-Token: o.PZl5XCp6SBl4F5PpaNXGDfFpUJZKAlEb" --header "Content-Type: application/json" --data-binary '{"type": "note", "title": "ALERT", "body": "$Message"}' --request POST https://api.pushbullet.com/v2/pushes exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/clone-repos.ps1 b/Scripts/clone-repos.ps1 index 2d178c20..33488b90 100755 --- a/Scripts/clone-repos.ps1 +++ b/Scripts/clone-repos.ps1 @@ -8,12 +8,17 @@ $Repos = "https://github.com/commonmark/cmark", "https://github.com/opencv/opencv", "https://github.com/openzfs/zfs", "https://github.com/synesthesiam/voice2json", "https://github.com/fleschutz/CWTS", "https://github.com/fleschutz/cubesum", "https://github.com/fleschutz/PowerShell","https://github.com/fleschutz/CWTS", "https://github.com/fleschutz/eventdriven" -foreach ($Repo in $Repos) { - $Answer = read-host "Do you want to clone $Repo (y/n)" - if ($Answer -eq "y") { - git clone $Repo +try { + foreach ($Repo in $Repos) { + $Answer = read-host "Do you want to clone $Repo (y/n)" + if ($Answer -eq "y") { + git clone $Repo + } } -} -write-host "Done." -exit 0 + write-host "Done." + exit 0 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/configure-git.ps1 b/Scripts/configure-git.ps1 index 1c0df533..25655090 100755 --- a/Scripts/configure-git.ps1 +++ b/Scripts/configure-git.ps1 @@ -18,5 +18,7 @@ try { git config --global init.defaultBranch main echo "Done." exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/download.ps1 b/Scripts/download.ps1 index a3628a02..647fff0e 100755 --- a/Scripts/download.ps1 +++ b/Scripts/download.ps1 @@ -15,5 +15,7 @@ try { wget --mirror --convert-links --adjust-extension --page-requisites --no-parent $URL --directory-prefix . --no-verbose write-host "✔️ Done." exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/inspect-exe.ps1 b/Scripts/inspect-exe.ps1 index f81d3894..5b129930 100755 --- a/Scripts/inspect-exe.ps1 +++ b/Scripts/inspect-exe.ps1 @@ -15,5 +15,7 @@ if ($File -eq "" ) { try { get-childitem $File | % {$_.VersionInfo} | Select * exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/latlong.ps1 b/Scripts/latlong.ps1 index 1fd6883e..bdc4f45d 100755 --- a/Scripts/latlong.ps1 +++ b/Scripts/latlong.ps1 @@ -32,5 +32,7 @@ try { } write-error "City $City not found" exit 1 -} catch { write-error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/list-cmdlets.ps1 b/Scripts/list-cmdlets.ps1 index 7f49b06e..138e755c 100755 --- a/Scripts/list-cmdlets.ps1 +++ b/Scripts/list-cmdlets.ps1 @@ -9,5 +9,7 @@ try { Get-Command -Command-Type cmdlet exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/list-empty-dirs.ps1 b/Scripts/list-empty-dirs.ps1 index 4a99a0f1..58ff00c8 100755 --- a/Scripts/list-empty-dirs.ps1 +++ b/Scripts/list-empty-dirs.ps1 @@ -14,5 +14,7 @@ try { (Get-ChildItem $DirTree -recurse | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName echo "Done." exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/list-modules.ps1 b/Scripts/list-modules.ps1 index 243a7c6b..01f87801 100755 --- a/Scripts/list-modules.ps1 +++ b/Scripts/list-modules.ps1 @@ -9,5 +9,7 @@ try { Get-Module exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/list-processes.ps1 b/Scripts/list-processes.ps1 index 9cc6b4a3..26bd352c 100755 --- a/Scripts/list-processes.ps1 +++ b/Scripts/list-processes.ps1 @@ -9,5 +9,7 @@ try { Get-Process | Format-Table -Property Id, @{Label="CPU(s)";Expression={$_.CPU.ToString("N")+"%"};Alignment="Right"}, ProcessName -AutoSize exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/list-unused-files.ps1 b/Scripts/list-unused-files.ps1 index 4d4c0c85..25454216 100755 --- a/Scripts/list-unused-files.ps1 +++ b/Scripts/list-unused-files.ps1 @@ -16,5 +16,7 @@ try { Get-ChildItem -path $DirTree -recurse | Where-Object {$_.LastAccessTime -le $cutOffDate} | select fullname exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/make-install.ps1 b/Scripts/make-install.ps1 index 1b543e2b..d48757c8 100755 --- a/Scripts/make-install.ps1 +++ b/Scripts/make-install.ps1 @@ -20,5 +20,7 @@ try { echo. pause exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/moon.ps1 b/Scripts/moon.ps1 index 789394ef..0de1a413 100755 --- a/Scripts/moon.ps1 +++ b/Scripts/moon.ps1 @@ -9,5 +9,7 @@ try { (Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/news.ps1 b/Scripts/news.ps1 index 1c0ad3fb..a7057b4b 100755 --- a/Scripts/news.ps1 +++ b/Scripts/news.ps1 @@ -21,5 +21,7 @@ try { write-host "*" $item.title } exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/password.ps1 b/Scripts/password.ps1 index 7270d8c3..c87a292d 100755 --- a/Scripts/password.ps1 +++ b/Scripts/password.ps1 @@ -23,5 +23,7 @@ try { $password = new_password write-output $password exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/passwords.ps1 b/Scripts/passwords.ps1 index 1bf3c31f..69d87b69 100755 --- a/Scripts/passwords.ps1 +++ b/Scripts/passwords.ps1 @@ -26,5 +26,7 @@ try { write-output $password } exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/poweroff.ps1 b/Scripts/poweroff.ps1 index 5bd48901..c3eb1643 100755 --- a/Scripts/poweroff.ps1 +++ b/Scripts/poweroff.ps1 @@ -10,5 +10,7 @@ try { Stop-Computer exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/reboot.ps1 b/Scripts/reboot.ps1 index 698248c2..78ce292c 100755 --- a/Scripts/reboot.ps1 +++ b/Scripts/reboot.ps1 @@ -10,5 +10,7 @@ try { Restart-Computer exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/send-email.ps1 b/Scripts/send-email.ps1 index 0d2ed5a4..88bb3f7b 100755 --- a/Scripts/send-email.ps1 +++ b/Scripts/send-email.ps1 @@ -22,5 +22,7 @@ try { $msg.body = $Body $smtp.Send($msg) exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/send-udp.ps1 b/Scripts/send-udp.ps1 index b7bfd22c..8b191aee 100755 --- a/Scripts/send-udp.ps1 +++ b/Scripts/send-udp.ps1 @@ -28,5 +28,7 @@ try { $Socket.Close() echo "Done." exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/speak-file.ps1 b/Scripts/speak-file.ps1 index c46e7f8c..ca919a91 100755 --- a/Scripts/speak-file.ps1 +++ b/Scripts/speak-file.ps1 @@ -17,5 +17,7 @@ try { $voice = New-Object ComObject SAPI.SPVoice $voice.Speak($Text); exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/speak-text.ps1 b/Scripts/speak-text.ps1 index 1e9b750a..9dfd1223 100755 --- a/Scripts/speak-text.ps1 +++ b/Scripts/speak-text.ps1 @@ -15,5 +15,7 @@ try { $voice = New-Object ComObject SAPI.SPVoice $voice.Speak($Text); exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/switch-shelly1.ps1 b/Scripts/switch-shelly1.ps1 index 6d2fc383..aa4ab00a 100755 --- a/Scripts/switch-shelly1.ps1 +++ b/Scripts/switch-shelly1.ps1 @@ -23,5 +23,7 @@ try { write-host "OK - switched Shelly1 device at $IPaddr to $TurnMode for $Timer second(s)" exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/test.ps1 b/Scripts/test.ps1 index 7b194b44..5c733862 100755 --- a/Scripts/test.ps1 +++ b/Scripts/test.ps1 @@ -10,5 +10,7 @@ try { write-output "✔️ PowerShell works. Details are:" echo $PSVersionTable exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/train-dns-cache.ps1 b/Scripts/train-dns-cache.ps1 index 7a63ad21..8e1fe4da 100755 --- a/Scripts/train-dns-cache.ps1 +++ b/Scripts/train-dns-cache.ps1 @@ -21,5 +21,7 @@ try { $TimeInterval = New-Timespan -start $StartTime -end $StopTime write-host "OK - DNS cache trained with $Count domain names in $TimeInterval sec." exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/translate-text.ps1 b/Scripts/translate-text.ps1 index 83539a2a..9290e611 100755 --- a/Scripts/translate-text.ps1 +++ b/Scripts/translate-text.ps1 @@ -40,5 +40,7 @@ try { write-output $TargetLanguage" : "$Result } exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/txt2wav.ps1 b/Scripts/txt2wav.ps1 index 88ee88b6..e788446b 100755 --- a/Scripts/txt2wav.ps1 +++ b/Scripts/txt2wav.ps1 @@ -21,5 +21,7 @@ try { $SpeechSynthesizer.Speak($Text) $SpeechSynthesizer.Dispose() exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/wakeup.ps1 b/Scripts/wakeup.ps1 index e462776e..8d541c9b 100755 --- a/Scripts/wakeup.ps1 +++ b/Scripts/wakeup.ps1 @@ -61,5 +61,7 @@ try { echo "Sorry, hostname $Hostname is unknown." pause exit 1 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/weather-alert.ps1 b/Scripts/weather-alert.ps1 index 6fbb69aa..13765c20 100755 --- a/Scripts/weather-alert.ps1 +++ b/Scripts/weather-alert.ps1 @@ -33,5 +33,7 @@ try { echo "WEATHER ALERT: $Result" } exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/weather-report.ps1 b/Scripts/weather-report.ps1 index 35f9d138..d8763801 100755 --- a/Scripts/weather-report.ps1 +++ b/Scripts/weather-report.ps1 @@ -11,5 +11,7 @@ $GeoLocation="" # empty means determine automatically try { (Invoke-WebRequest http://v2d.wttr.in/$GeoLocation -UserAgent "curl" ).Content exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/weather-worldwide.ps1 b/Scripts/weather-worldwide.ps1 index 7f506ffe..d9c45726 100755 --- a/Scripts/weather-worldwide.ps1 +++ b/Scripts/weather-worldwide.ps1 @@ -15,5 +15,7 @@ try { (Invoke-WebRequest http://wttr.in/${City}?format="* %l:+%c+%t+%p+%h+%P+%w +%S ->+%s" -UserAgent "curl" ).Content } exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/weather.ps1 b/Scripts/weather.ps1 index 1dc06e50..5c7ad37c 100755 --- a/Scripts/weather.ps1 +++ b/Scripts/weather.ps1 @@ -11,5 +11,7 @@ $GeoLocation="" # empty means determine automatically try { (Invoke-WebRequest http://wttr.in/$GeoLocation -UserAgent "curl" ).Content exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/zip-dir.ps1 b/Scripts/zip-dir.ps1 index 53b72a7f..0474d29e 100755 --- a/Scripts/zip-dir.ps1 +++ b/Scripts/zip-dir.ps1 @@ -14,5 +14,7 @@ if ($Path -eq "" ) { try { Compress-Archive -Path $Path -DestinationPath $Path.zip exit 0 -} catch { Write-Error $Error[0] } -exit 1 +} catch { + Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +}