From 847be3777271775beac8abd3ebb69cf84689aed6 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Mon, 5 Oct 2020 13:12:12 +0200 Subject: [PATCH] Added try and catch --- Scripts/MD5.ps1 | 10 +++++++--- Scripts/SHA1.ps1 | 10 +++++++--- Scripts/SHA256.ps1 | 10 +++++++--- Scripts/download.ps1 | 9 +++++++-- Scripts/email.ps1 | 21 ++++++++++++--------- Scripts/exe_info.ps1 | 9 +++++++-- Scripts/init_git.ps1 | 13 ++++++++----- Scripts/lscmdlets.ps1 | 7 +++++-- Scripts/lsmods.ps1 | 7 +++++-- Scripts/lsproc.ps1 | 7 +++++-- Scripts/make-install.ps1 | 19 +++++++++++-------- Scripts/moon.ps1 | 7 +++++-- Scripts/news.ps1 | 19 +++++++++++-------- Scripts/password.ps1 | 9 ++++++--- Scripts/passwords.ps1 | 13 ++++++++----- Scripts/poweroff.ps1 | 7 +++++-- Scripts/reboot.ps1 | 7 +++++-- Scripts/speak.ps1 | 17 ++++++++++++----- Scripts/test.ps1 | 9 ++++++--- Scripts/train_dns_cache.ps1 | 25 ++++++++++++++----------- Scripts/translate.ps1 | 13 ++++++++----- Scripts/txt2wav.ps1 | 19 +++++++++++-------- Scripts/wakeup.ps1 | 25 ++++++++++++++----------- Scripts/weather.ps1 | 8 ++++++-- Scripts/zipdir.ps1 | 9 +++++++-- 25 files changed, 199 insertions(+), 110 deletions(-) diff --git a/Scripts/MD5.ps1 b/Scripts/MD5.ps1 index 586381fa..b32eeae3 100755 --- a/Scripts/MD5.ps1 +++ b/Scripts/MD5.ps1 @@ -7,6 +7,10 @@ # License: CC0 param([string]$File) -$Result = get-filehash $File -algorithm MD5 -write-host $Result.Hash -exit 0 + +try { + $Result = get-filehash $File -algorithm MD5 + write-host $Result.Hash + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/SHA1.ps1 b/Scripts/SHA1.ps1 index 0f7aa9fe..be618355 100755 --- a/Scripts/SHA1.ps1 +++ b/Scripts/SHA1.ps1 @@ -7,6 +7,10 @@ # License: CC0 param([string]$File) -$Result = get-filehash $File -algorithm SHA1 -write-host $Result.Hash -exit 0 + +try { + $Result = get-filehash $File -algorithm SHA1 + write-host $Result.Hash + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/SHA256.ps1 b/Scripts/SHA256.ps1 index a9276817..8da5d62f 100755 --- a/Scripts/SHA256.ps1 +++ b/Scripts/SHA256.ps1 @@ -7,6 +7,10 @@ # License: CC0 param([string]$File) -$Result = get-filehash $File -algorithm SHA256 -write-host $Result.Hash -exit 0 + +try { + $Result = get-filehash $File -algorithm SHA256 + write-host $Result.Hash + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/download.ps1 b/Scripts/download.ps1 index 46038ca5..4bc5b930 100755 --- a/Scripts/download.ps1 +++ b/Scripts/download.ps1 @@ -7,5 +7,10 @@ # License: CC0 param([string]$URL) -wget --mirror --convert-links --adjust-extension --page-requisites --no-parent $URL --directory-prefix . --no-verbose -exit 0 + + +try { + wget --mirror --convert-links --adjust-extension --page-requisites --no-parent $URL --directory-prefix . --no-verbose + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/email.ps1 b/Scripts/email.ps1 index 5518e287..70ffeb9e 100755 --- a/Scripts/email.ps1 +++ b/Scripts/email.ps1 @@ -6,12 +6,15 @@ # Source: github.com/fleschutz/PowerShell # License: CC0 -$smtpServer = "smtp.example.com" -$msg = new-object Net.Mail.MailMessage -$smtp = new-object Net.Mail.SmtpClient($smtpServer) -$msg.From = "me@example.com" -$msg.ReplyTo = "me@example.com" -$msg.To.Add("you@example.com") -$msg.subject = "Test Mail" -$msg.body = "This is a test mail." -$smtp.Send($msg) +try { + $smtpServer = "smtp.example.com" + $msg = new-object Net.Mail.MailMessage + $smtp = new-object Net.Mail.SmtpClient($smtpServer) + $msg.From = "me@example.com" + $msg.ReplyTo = "me@example.com" + $msg.To.Add("you@example.com") + $msg.subject = "Test Mail" + $msg.body = "This is a test mail." + $smtp.Send($msg) +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/exe_info.ps1 b/Scripts/exe_info.ps1 index 16b8eb14..560d2d20 100755 --- a/Scripts/exe_info.ps1 +++ b/Scripts/exe_info.ps1 @@ -7,5 +7,10 @@ # License: CC0 param([string]$File) -get-childitem $File | % {$_.VersionInfo} | Select * -exit 0 + + +try { + get-childitem $File | % {$_.VersionInfo} | Select * + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/init_git.ps1 b/Scripts/init_git.ps1 index ea20f782..fd25d604 100755 --- a/Scripts/init_git.ps1 +++ b/Scripts/init_git.ps1 @@ -10,8 +10,11 @@ $UserName = read-host "Your full name: " $UserEmail = read-host "Your email address: " $UserEditor = read-host "Your favorite editor (nano, vi, emacs): " -git config --global user.name $UserName -git config --global user.email $UserEmail -git config --global core.editor $UserEditor -echo "Done." -exit 0 +try { + git config --global user.name $UserName + git config --global user.email $UserEmail + git config --global core.editor $UserEditor + echo "Done. Git has been initialized." + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/lscmdlets.ps1 b/Scripts/lscmdlets.ps1 index 7ba31e23..3db5d7ac 100755 --- a/Scripts/lscmdlets.ps1 +++ b/Scripts/lscmdlets.ps1 @@ -6,5 +6,8 @@ # Source: github.com/fleschutz/PowerShell # License: CC0 -Get-Command -Command-Type cmdlet -exit 0 +try { + Get-Command -Command-Type cmdlet + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/lsmods.ps1 b/Scripts/lsmods.ps1 index 2cd31734..aa1fa869 100755 --- a/Scripts/lsmods.ps1 +++ b/Scripts/lsmods.ps1 @@ -6,5 +6,8 @@ # Source: github.com/fleschutz/PowerShell # License: CC0 -Get-Module -exit 0 +try { + Get-Module + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/lsproc.ps1 b/Scripts/lsproc.ps1 index d2390c7a..cc643767 100755 --- a/Scripts/lsproc.ps1 +++ b/Scripts/lsproc.ps1 @@ -6,5 +6,8 @@ # Source: github.com/fleschutz/PowerShell # License: CC0 -Get-Process | Format-Table -Property Id, @{Label="CPU(s)";Expression={$_.CPU.ToString("N")+"%"};Alignment="Right"}, ProcessName -AutoSize -exit 0 +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 diff --git a/Scripts/make-install.ps1 b/Scripts/make-install.ps1 index 9ee7ca39..794b1d11 100755 --- a/Scripts/make-install.ps1 +++ b/Scripts/make-install.ps1 @@ -10,12 +10,15 @@ set "DST_DIR=C:\Program Files\MyApp\bin" set FILTER=*.exe *.dll set OPTIONS=/E /njh /np -title Syncing to %DST_DIR% ... -robocopy %SRC_DIR% %DST_DIR% %FILTER% %OPTIONS% +try { + title Syncing to %DST_DIR% ... + robocopy %SRC_DIR% %DST_DIR% %FILTER% %OPTIONS% -echo ------------------------------------------------------------------------------ -echo. -echo DONE - synced to %DST_DIR% -echo. -pause -exit /b 0 + echo ------------------------------------------------------------------------------ + echo. + echo DONE - synced to %DST_DIR% + echo. + pause + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/moon.ps1 b/Scripts/moon.ps1 index a68a8840..929b109d 100755 --- a/Scripts/moon.ps1 +++ b/Scripts/moon.ps1 @@ -6,5 +6,8 @@ # Source: github.com/fleschutz/PowerShell # License: CC0 -(Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content -exit 0 +try { + (Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/news.ps1 b/Scripts/news.ps1 index 90af0edb..15a97ad5 100755 --- a/Scripts/news.ps1 +++ b/Scripts/news.ps1 @@ -10,13 +10,16 @@ $RSS_URL = "https://yahoo.com/news/rss/world" # $RSS_URL = "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml" -[xml]$FileContent = (Invoke-WebRequest -Uri $RSS_URL).Content +try { + [xml]$FileContent = (Invoke-WebRequest -Uri $RSS_URL).Content -write-host "" -write-host "+++ " $FileContent.rss.channel.title " +++" -write-host "" + write-host "" + write-host "+++ " $FileContent.rss.channel.title " +++" + write-host "" -foreach ($item in $FileContent.rss.channel.item) { - write-host "* "$item.title -} -exit 0 + foreach ($item in $FileContent.rss.channel.item) { + write-host "* "$item.title + } + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/password.ps1 b/Scripts/password.ps1 index 74950c17..967a084c 100755 --- a/Scripts/password.ps1 +++ b/Scripts/password.ps1 @@ -19,6 +19,9 @@ function new_password() { return $password } -$password = new_password -write-output $password -exit 0 +try { + $password = new_password + write-output $password + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/passwords.ps1 b/Scripts/passwords.ps1 index c3d7d8be..134279cf 100755 --- a/Scripts/passwords.ps1 +++ b/Scripts/passwords.ps1 @@ -20,8 +20,11 @@ function new_password() { return $password } -for ($j = 0; $j -lt $NumPasswords; $j++) { - $password = new_password - write-output $password -} -exit 0 +try { + for ($j = 0; $j -lt $NumPasswords; $j++) { + $password = new_password + write-output $password + } + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/poweroff.ps1 b/Scripts/poweroff.ps1 index 549449de..8f99caff 100755 --- a/Scripts/poweroff.ps1 +++ b/Scripts/poweroff.ps1 @@ -7,5 +7,8 @@ # License: CC0 #Requires -RunAsAdministrator -Stop-Computer -exit 0 +try { + Stop-Computer + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/reboot.ps1 b/Scripts/reboot.ps1 index 971d5008..03d51f9f 100755 --- a/Scripts/reboot.ps1 +++ b/Scripts/reboot.ps1 @@ -7,5 +7,8 @@ # License: CC0 #Requires -RunAsAdministrator -Restart-Computer -exit 0 +try { + Restart-Computer + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/speak.ps1 b/Scripts/speak.ps1 index bc034a14..ea7aa93d 100755 --- a/Scripts/speak.ps1 +++ b/Scripts/speak.ps1 @@ -1,12 +1,19 @@ #!/snap/bin/powershell # -# Syntax: ./speak.ps1 +# Syntax: ./speak.ps1 [] # Description: speaks the given text # Author: Markus Fleschutz # Source: github.com/fleschutz/PowerShell # License: CC0 -$Text = "Hello World!" -$voice = New-Object ComObject SAPI.SPVoice -$voice.Speak($Text); -exit 0 +param([string]$Text) +if ($Text -eq "") { + $Text = "Hello World!" +} + +try { + $voice = New-Object ComObject SAPI.SPVoice + $voice.Speak($Text); + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/test.ps1 b/Scripts/test.ps1 index 83cb6e82..56809001 100755 --- a/Scripts/test.ps1 +++ b/Scripts/test.ps1 @@ -6,6 +6,9 @@ # Source: github.com/fleschutz/PowerShell # License: CC0 -write-output "✔️ PowerShell works. Details are:" -echo $PSVersionTable -exit 0 +try { + write-output "✔️ PowerShell works. Details are:" + echo $PSVersionTable + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/train_dns_cache.ps1 b/Scripts/train_dns_cache.ps1 index dcb4781d..38f61688 100755 --- a/Scripts/train_dns_cache.ps1 +++ b/Scripts/train_dns_cache.ps1 @@ -8,15 +8,18 @@ $Table = import-csv domain_table.csv -$StartTime = Get-Date -foreach($Row in $Table) { - $Domain = $Row.Domain - write-progress "Training DNS cache with $Domain ..." - $Ignore = nslookup $Domain -} +try { + $StartTime = Get-Date + foreach($Row in $Table) { + $Domain = $Row.Domain + write-progress "Training DNS cache with $Domain ..." + $Ignore = nslookup $Domain + } -$Count = $Table.Length -$StopTime = Get-Date -$TimeInterval = New-Timespan -start $StartTime -end $StopTime -write-host "✔️ DNS cache trained with $Count domain names in $TimeInterval sec." -exit 0 + $Count = $Table.Length + $StopTime = Get-Date + $TimeInterval = New-Timespan -start $StartTime -end $StopTime + write-host "✔️ DNS cache trained with $Count domain names in $TimeInterval sec." + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/translate.ps1 b/Scripts/translate.ps1 index fe77ed73..e8bd7bfc 100755 --- a/Scripts/translate.ps1 +++ b/Scripts/translate.ps1 @@ -31,8 +31,11 @@ function TranslateWithGoogle { return $result[0][0][0] } -foreach($TargetLang in $TargetLanguages) { - $Result = TranslateWithGoogle $SourceText $SourceLang $TargetLang - write-output $TargetLang" : "$Result -} -exit 0 +try { + foreach($TargetLang in $TargetLanguages) { + $Result = TranslateWithGoogle $SourceText $SourceLang $TargetLang + write-output $TargetLang" : "$Result + } + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/txt2wav.ps1 b/Scripts/txt2wav.ps1 index fd5b2d46..93aa6ddb 100755 --- a/Scripts/txt2wav.ps1 +++ b/Scripts/txt2wav.ps1 @@ -12,11 +12,14 @@ $Speed = -1 # -10 is slowest, 10 is fastest $TargetWavFile = "spoken.wav" # Run: -Add-Type -AssemblyName System.Speech -$SpeechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer -# $SpeechSynthesizer.SelectVoice("Microsoft Eva Mobile") -$SpeechSynthesizer.Rate = $Speed -$SpeechSynthesizer.SetOutputToWaveFile($TargetWavFile) -$SpeechSynthesizer.Speak($Text) -$SpeechSynthesizer.Dispose() -exit 0 +try { + Add-Type -AssemblyName System.Speech + $SpeechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer + # $SpeechSynthesizer.SelectVoice("Microsoft Eva Mobile") + $SpeechSynthesizer.Rate = $Speed + $SpeechSynthesizer.SetOutputToWaveFile($TargetWavFile) + $SpeechSynthesizer.Speak($Text) + $SpeechSynthesizer.Dispose() + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/wakeup.ps1 b/Scripts/wakeup.ps1 index d12897ff..2855ebec 100755 --- a/Scripts/wakeup.ps1 +++ b/Scripts/wakeup.ps1 @@ -42,17 +42,20 @@ $UDPclient.Connect($broadcast,$port) $Hostname = read-host "Enter hostname: " $MyMACAddresses = "io=11:22:33:44:55:66","pi=11:22:33:44:55:66" -foreach($item in $MyMACAddresses) { - $array = $item.Split("=") - $thisHost=$array[0] - $thisMAC=$array[1] - if ($thisHost -like $Hostname) { - Send-WOL $thisMAC - write-output "✔️ host $thisHost waked up (MAC $thisMAC)" - exit 0 +try { + foreach($item in $MyMACAddresses) { + $array = $item.Split("=") + $thisHost=$array[0] + $thisMAC=$array[1] + if ($thisHost -like $Hostname) { + Send-WOL $thisMAC + write-output "✔️ host $thisHost waked up (MAC $thisMAC)" + exit 0 + } } -} -echo "Sorry, hostname $Hostname is unknown." -pause + echo "Sorry, hostname $Hostname is unknown." + pause + exit 1 +} catch { Write-Error $Error[0] } exit 1 diff --git a/Scripts/weather.ps1 b/Scripts/weather.ps1 index 3596d669..8c8d6856 100755 --- a/Scripts/weather.ps1 +++ b/Scripts/weather.ps1 @@ -7,5 +7,9 @@ # License: CC0 $GeoLocation="" # empty means determine automatically -(Invoke-WebRequest http://wttr.in/$GeoLocation -UserAgent "curl" ).Content -exit 0 + +try { + (Invoke-WebRequest http://wttr.in/$GeoLocation -UserAgent "curl" ).Content + exit 0 +} catch { Write-Error $Error[0] } +exit 1 diff --git a/Scripts/zipdir.ps1 b/Scripts/zipdir.ps1 index f32e3e42..f36e711e 100755 --- a/Scripts/zipdir.ps1 +++ b/Scripts/zipdir.ps1 @@ -7,5 +7,10 @@ # License: CC0 param([string]$Path) -Compress-Archive -Path $Path -DestinationPath $Path.zip -exit 0 + + +try { + Compress-Archive -Path $Path -DestinationPath $Path.zip + exit 0 +} catch { Write-Error $Error[0] } +exit 1