Added try and catch

This commit is contained in:
Markus Fleschutz 2020-10-05 13:12:12 +02:00
parent 14ab054646
commit 847be37772
25 changed files with 199 additions and 110 deletions

View File

@ -7,6 +7,10 @@
# License: CC0 # License: CC0
param([string]$File) param([string]$File)
$Result = get-filehash $File -algorithm MD5
write-host $Result.Hash try {
exit 0 $Result = get-filehash $File -algorithm MD5
write-host $Result.Hash
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,6 +7,10 @@
# License: CC0 # License: CC0
param([string]$File) param([string]$File)
$Result = get-filehash $File -algorithm SHA1
write-host $Result.Hash try {
exit 0 $Result = get-filehash $File -algorithm SHA1
write-host $Result.Hash
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,6 +7,10 @@
# License: CC0 # License: CC0
param([string]$File) param([string]$File)
$Result = get-filehash $File -algorithm SHA256
write-host $Result.Hash try {
exit 0 $Result = get-filehash $File -algorithm SHA256
write-host $Result.Hash
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,5 +7,10 @@
# License: CC0 # License: CC0
param([string]$URL) 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

View File

@ -6,12 +6,15 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
$smtpServer = "smtp.example.com" try {
$msg = new-object Net.Mail.MailMessage $smtpServer = "smtp.example.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer) $msg = new-object Net.Mail.MailMessage
$msg.From = "me@example.com" $smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.ReplyTo = "me@example.com" $msg.From = "me@example.com"
$msg.To.Add("you@example.com") $msg.ReplyTo = "me@example.com"
$msg.subject = "Test Mail" $msg.To.Add("you@example.com")
$msg.body = "This is a test mail." $msg.subject = "Test Mail"
$smtp.Send($msg) $msg.body = "This is a test mail."
$smtp.Send($msg)
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,5 +7,10 @@
# License: CC0 # License: CC0
param([string]$File) 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

View File

@ -10,8 +10,11 @@ $UserName = read-host "Your full name: "
$UserEmail = read-host "Your email address: " $UserEmail = read-host "Your email address: "
$UserEditor = read-host "Your favorite editor (nano, vi, emacs): " $UserEditor = read-host "Your favorite editor (nano, vi, emacs): "
git config --global user.name $UserName try {
git config --global user.email $UserEmail git config --global user.name $UserName
git config --global core.editor $UserEditor git config --global user.email $UserEmail
echo "Done." git config --global core.editor $UserEditor
exit 0 echo "Done. Git has been initialized."
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -6,5 +6,8 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
Get-Command -Command-Type cmdlet try {
exit 0 Get-Command -Command-Type cmdlet
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -6,5 +6,8 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
Get-Module try {
exit 0 Get-Module
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -6,5 +6,8 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
Get-Process | Format-Table -Property Id, @{Label="CPU(s)";Expression={$_.CPU.ToString("N")+"%"};Alignment="Right"}, ProcessName -AutoSize try {
exit 0 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

View File

@ -10,12 +10,15 @@ set "DST_DIR=C:\Program Files\MyApp\bin"
set FILTER=*.exe *.dll set FILTER=*.exe *.dll
set OPTIONS=/E /njh /np set OPTIONS=/E /njh /np
title Syncing to %DST_DIR% ... try {
robocopy %SRC_DIR% %DST_DIR% %FILTER% %OPTIONS% title Syncing to %DST_DIR% ...
robocopy %SRC_DIR% %DST_DIR% %FILTER% %OPTIONS%
echo ------------------------------------------------------------------------------ echo ------------------------------------------------------------------------------
echo. echo.
echo DONE - synced to %DST_DIR% echo DONE - synced to %DST_DIR%
echo. echo.
pause pause
exit /b 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -6,5 +6,8 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
(Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content try {
exit 0 (Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -10,13 +10,16 @@
$RSS_URL = "https://yahoo.com/news/rss/world" $RSS_URL = "https://yahoo.com/news/rss/world"
# $RSS_URL = "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml" # $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 ""
write-host "+++ " $FileContent.rss.channel.title " +++" write-host "+++ " $FileContent.rss.channel.title " +++"
write-host "" write-host ""
foreach ($item in $FileContent.rss.channel.item) { foreach ($item in $FileContent.rss.channel.item) {
write-host "* "$item.title write-host "* "$item.title
} }
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -19,6 +19,9 @@ function new_password() {
return $password return $password
} }
$password = new_password try {
write-output $password $password = new_password
exit 0 write-output $password
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -20,8 +20,11 @@ function new_password() {
return $password return $password
} }
for ($j = 0; $j -lt $NumPasswords; $j++) { try {
for ($j = 0; $j -lt $NumPasswords; $j++) {
$password = new_password $password = new_password
write-output $password write-output $password
} }
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,5 +7,8 @@
# License: CC0 # License: CC0
#Requires -RunAsAdministrator #Requires -RunAsAdministrator
Stop-Computer try {
exit 0 Stop-Computer
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,5 +7,8 @@
# License: CC0 # License: CC0
#Requires -RunAsAdministrator #Requires -RunAsAdministrator
Restart-Computer try {
exit 0 Restart-Computer
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -1,12 +1,19 @@
#!/snap/bin/powershell #!/snap/bin/powershell
# #
# Syntax: ./speak.ps1 # Syntax: ./speak.ps1 [<text>]
# Description: speaks the given text # Description: speaks the given text
# Author: Markus Fleschutz # Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
$Text = "Hello World!" param([string]$Text)
$voice = New-Object ComObject SAPI.SPVoice if ($Text -eq "") {
$voice.Speak($Text); $Text = "Hello World!"
exit 0 }
try {
$voice = New-Object ComObject SAPI.SPVoice
$voice.Speak($Text);
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -6,6 +6,9 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
write-output "✔️ PowerShell works. Details are:" try {
echo $PSVersionTable write-output "✔️ PowerShell works. Details are:"
exit 0 echo $PSVersionTable
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -8,15 +8,18 @@
$Table = import-csv domain_table.csv $Table = import-csv domain_table.csv
$StartTime = Get-Date try {
foreach($Row in $Table) { $StartTime = Get-Date
foreach($Row in $Table) {
$Domain = $Row.Domain $Domain = $Row.Domain
write-progress "Training DNS cache with $Domain ..." write-progress "Training DNS cache with $Domain ..."
$Ignore = nslookup $Domain $Ignore = nslookup $Domain
} }
$Count = $Table.Length $Count = $Table.Length
$StopTime = Get-Date $StopTime = Get-Date
$TimeInterval = New-Timespan -start $StartTime -end $StopTime $TimeInterval = New-Timespan -start $StartTime -end $StopTime
write-host "✔️ DNS cache trained with $Count domain names in $TimeInterval sec." write-host "✔️ DNS cache trained with $Count domain names in $TimeInterval sec."
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -31,8 +31,11 @@ function TranslateWithGoogle {
return $result[0][0][0] return $result[0][0][0]
} }
foreach($TargetLang in $TargetLanguages) { try {
foreach($TargetLang in $TargetLanguages) {
$Result = TranslateWithGoogle $SourceText $SourceLang $TargetLang $Result = TranslateWithGoogle $SourceText $SourceLang $TargetLang
write-output $TargetLang" : "$Result write-output $TargetLang" : "$Result
} }
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -12,11 +12,14 @@ $Speed = -1 # -10 is slowest, 10 is fastest
$TargetWavFile = "spoken.wav" $TargetWavFile = "spoken.wav"
# Run: # Run:
Add-Type -AssemblyName System.Speech try {
$SpeechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer Add-Type -AssemblyName System.Speech
# $SpeechSynthesizer.SelectVoice("Microsoft Eva Mobile") $SpeechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer
$SpeechSynthesizer.Rate = $Speed # $SpeechSynthesizer.SelectVoice("Microsoft Eva Mobile")
$SpeechSynthesizer.SetOutputToWaveFile($TargetWavFile) $SpeechSynthesizer.Rate = $Speed
$SpeechSynthesizer.Speak($Text) $SpeechSynthesizer.SetOutputToWaveFile($TargetWavFile)
$SpeechSynthesizer.Dispose() $SpeechSynthesizer.Speak($Text)
exit 0 $SpeechSynthesizer.Dispose()
exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -42,7 +42,8 @@ $UDPclient.Connect($broadcast,$port)
$Hostname = read-host "Enter hostname: " $Hostname = read-host "Enter hostname: "
$MyMACAddresses = "io=11:22:33:44:55:66","pi=11:22:33:44:55:66" $MyMACAddresses = "io=11:22:33:44:55:66","pi=11:22:33:44:55:66"
foreach($item in $MyMACAddresses) { try {
foreach($item in $MyMACAddresses) {
$array = $item.Split("=") $array = $item.Split("=")
$thisHost=$array[0] $thisHost=$array[0]
$thisMAC=$array[1] $thisMAC=$array[1]
@ -51,8 +52,10 @@ foreach($item in $MyMACAddresses) {
write-output "✔️ host $thisHost waked up (MAC $thisMAC)" write-output "✔️ host $thisHost waked up (MAC $thisMAC)"
exit 0 exit 0
} }
} }
echo "Sorry, hostname $Hostname is unknown." echo "Sorry, hostname $Hostname is unknown."
pause pause
exit 1
} catch { Write-Error $Error[0] }
exit 1 exit 1

View File

@ -7,5 +7,9 @@
# License: CC0 # License: CC0
$GeoLocation="" # empty means determine automatically $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

View File

@ -7,5 +7,10 @@
# License: CC0 # License: CC0
param([string]$Path) 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