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
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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

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

View File

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

View File

@ -1,12 +1,19 @@
#!/snap/bin/powershell
#
# Syntax: ./speak.ps1
# Syntax: ./speak.ps1 [<text>]
# 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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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