Created
April 13, 2024 19:56
-
-
Save HauptJ/55ed6414c21904737f26c9007a4b8257 to your computer and use it in GitHub Desktop.
PowerShell Hash Table Example with CLI Parameters and Try Catch Error Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Input CLI params with default values | |
param ( | |
$number=1, | |
$shape="Square", | |
$color="Blue" | |
) | |
try { | |
# Ordered hash table | |
[hashtable]$hash = [ordered]@{Number = $number; Shape = $shape; Color = $color} | |
# print out hash table using Out-String | |
$hash | Out-String | Write-Host | |
# print out hash table using for loop | |
foreach ($h in $hash.GetEnumerator() ) { | |
Write-Host "$($h.Name) : $($h.Value)" | |
} | |
} catch { | |
Write-Host "An error occured" | |
Exit 1 | |
} finally { | |
Write-Host "This script executed successfully" | |
Exit 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment