Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PlagueHO/a7e99e85236592099d1d8e33fc532ab6 to your computer and use it in GitHub Desktop.
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
$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