Last active
February 10, 2019 16:12
-
-
Save nohwnd/c97368c7ee2cce5ba461b6f564ed9149 to your computer and use it in GitHub Desktop.
Add or update to hashtable
This file contains hidden or 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
function getOrUpdateValue { | |
[CmdletBinding()] | |
param( | |
$Hashtable, | |
$Key, | |
$DefaultValue | |
) | |
if ($Hashtable.ContainsKey($Key)) { | |
# do not enumerate so we get the same thing back | |
# even if it is a collection | |
$PSCmdlet.WriteObject($Hashtable.$Key, $false) | |
} | |
else { | |
$Hashtable.Add($Key, $DefaultValue) | |
# do not enumerate so we get the same thing back | |
# even if it is a collection | |
$PSCmdlet.WriteObject($DefaultValue, $false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment