Created
August 31, 2019 21:02
-
-
Save PlagueHO/a7e99e85236592099d1d8e33fc532ab6 to your computer and use it in GitHub Desktop.
Invoke an action on a file using a hash table of actions and null coalescing to report invalid action
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
$VerbosePreference = 'Continue' | |
$action = 'New' | |
$path = 'c:\somefile.txt' | |
$actions = @{ | |
'New' = { | |
Write-Verbose -Message 'Execute New-Item' | |
New-Item -Path $path | |
} | |
'Remove' = { | |
Write-Verbose -Message 'Execute Remove-Item' | |
Remove-Item -Path $path | |
} | |
'Get' = { | |
Write-Verbose -Message 'Execute Get-Item' | |
Get-Item -Path $path | |
} | |
} | |
return ($actions[$action], {Write-Verbose -Message 'Invalid Action'}, 1 -ne $null)[0].Invoke() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment