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)
try {
$Result = get-filehash $File -algorithm MD5 $Result = get-filehash $File -algorithm MD5
write-host $Result.Hash write-host $Result.Hash
exit 0 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)
try {
$Result = get-filehash $File -algorithm SHA1 $Result = get-filehash $File -algorithm SHA1
write-host $Result.Hash write-host $Result.Hash
exit 0 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)
try {
$Result = get-filehash $File -algorithm SHA256 $Result = get-filehash $File -algorithm SHA256
write-host $Result.Hash write-host $Result.Hash
exit 0 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)
try {
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent $URL --directory-prefix . --no-verbose wget --mirror --convert-links --adjust-extension --page-requisites --no-parent $URL --directory-prefix . --no-verbose
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -6,6 +6,7 @@
# Source: github.com/fleschutz/PowerShell # Source: github.com/fleschutz/PowerShell
# License: CC0 # License: CC0
try {
$smtpServer = "smtp.example.com" $smtpServer = "smtp.example.com"
$msg = new-object Net.Mail.MailMessage $msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp = new-object Net.Mail.SmtpClient($smtpServer)
@ -15,3 +16,5 @@ $msg.To.Add("you@example.com")
$msg.subject = "Test Mail" $msg.subject = "Test Mail"
$msg.body = "This is a test mail." $msg.body = "This is a test mail."
$smtp.Send($msg) $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)
try {
get-childitem $File | % {$_.VersionInfo} | Select * get-childitem $File | % {$_.VersionInfo} | Select *
exit 0 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): "
try {
git config --global user.name $UserName git config --global user.name $UserName
git config --global user.email $UserEmail git config --global user.email $UserEmail
git config --global core.editor $UserEditor git config --global core.editor $UserEditor
echo "Done." echo "Done. Git has been initialized."
exit 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
try {
Get-Command -Command-Type cmdlet Get-Command -Command-Type cmdlet
exit 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
try {
Get-Module Get-Module
exit 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
try {
Get-Process | Format-Table -Property Id, @{Label="CPU(s)";Expression={$_.CPU.ToString("N")+"%"};Alignment="Right"}, ProcessName -AutoSize Get-Process | Format-Table -Property Id, @{Label="CPU(s)";Expression={$_.CPU.ToString("N")+"%"};Alignment="Right"}, ProcessName -AutoSize
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -10,6 +10,7 @@ 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
try {
title Syncing to %DST_DIR% ... title Syncing to %DST_DIR% ...
robocopy %SRC_DIR% %DST_DIR% %FILTER% %OPTIONS% robocopy %SRC_DIR% %DST_DIR% %FILTER% %OPTIONS%
@ -18,4 +19,6 @@ 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
try {
(Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content (Invoke-WebRequest http://wttr.in/Moon -UserAgent "curl" ).Content
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -10,6 +10,7 @@
$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"
try {
[xml]$FileContent = (Invoke-WebRequest -Uri $RSS_URL).Content [xml]$FileContent = (Invoke-WebRequest -Uri $RSS_URL).Content
write-host "" write-host ""
@ -20,3 +21,5 @@ 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
} }
try {
$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

@ -20,8 +20,11 @@ function new_password() {
return $password return $password
} }
try {
for ($j = 0; $j -lt $NumPasswords; $j++) { 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
try {
Stop-Computer Stop-Computer
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
try {
Restart-Computer Restart-Computer
exit 0 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
param([string]$Text)
if ($Text -eq "") {
$Text = "Hello World!" $Text = "Hello World!"
}
try {
$voice = New-Object ComObject SAPI.SPVoice $voice = New-Object ComObject SAPI.SPVoice
$voice.Speak($Text); $voice.Speak($Text);
exit 0 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
try {
write-output "✔️ PowerShell works. Details are:" write-output "✔️ PowerShell works. Details are:"
echo $PSVersionTable echo $PSVersionTable
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -8,6 +8,7 @@
$Table = import-csv domain_table.csv $Table = import-csv domain_table.csv
try {
$StartTime = Get-Date $StartTime = Get-Date
foreach($Row in $Table) { foreach($Row in $Table) {
$Domain = $Row.Domain $Domain = $Row.Domain
@ -20,3 +21,5 @@ $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]
} }
try {
foreach($TargetLang in $TargetLanguages) { 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,6 +12,7 @@ $Speed = -1 # -10 is slowest, 10 is fastest
$TargetWavFile = "spoken.wav" $TargetWavFile = "spoken.wav"
# Run: # Run:
try {
Add-Type -AssemblyName System.Speech Add-Type -AssemblyName System.Speech
$SpeechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer $SpeechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer
# $SpeechSynthesizer.SelectVoice("Microsoft Eva Mobile") # $SpeechSynthesizer.SelectVoice("Microsoft Eva Mobile")
@ -20,3 +21,5 @@ $SpeechSynthesizer.SetOutputToWaveFile($TargetWavFile)
$SpeechSynthesizer.Speak($Text) $SpeechSynthesizer.Speak($Text)
$SpeechSynthesizer.Dispose() $SpeechSynthesizer.Dispose()
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1

View File

@ -42,6 +42,7 @@ $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"
try {
foreach($item in $MyMACAddresses) { foreach($item in $MyMACAddresses) {
$array = $item.Split("=") $array = $item.Split("=")
$thisHost=$array[0] $thisHost=$array[0]
@ -56,3 +57,5 @@ foreach($item in $MyMACAddresses) {
echo "Sorry, hostname $Hostname is unknown." echo "Sorry, hostname $Hostname is unknown."
pause pause
exit 1 exit 1
} catch { Write-Error $Error[0] }
exit 1

View File

@ -7,5 +7,9 @@
# License: CC0 # License: CC0
$GeoLocation="" # empty means determine automatically $GeoLocation="" # empty means determine automatically
try {
(Invoke-WebRequest http://wttr.in/$GeoLocation -UserAgent "curl" ).Content (Invoke-WebRequest http://wttr.in/$GeoLocation -UserAgent "curl" ).Content
exit 0 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)
try {
Compress-Archive -Path $Path -DestinationPath $Path.zip Compress-Archive -Path $Path -DestinationPath $Path.zip
exit 0 exit 0
} catch { Write-Error $Error[0] }
exit 1