Created
January 31, 2020 09:21
-
-
Save Splaxi/ec4667fd83bf4cd995e0e248f42612d2 to your computer and use it in GitHub Desktop.
Convert Hashtable to string to share hashtable with others
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
function Convert-HashTableToString { | |
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidDefaultValueSwitchParameter", "")] | |
[CmdletBinding()] | |
[OutputType([System.String])] | |
param ( | |
[HashTable] $InputObject, | |
[string] $KeyPrefix = "", | |
[string] $ValuePrefix = " = ", | |
[switch] $KeepCase = $true | |
) | |
$output = New-Object System.Collections.Generic.List[string] | |
$output.Add("`$parms = @{") | |
foreach ($key in $InputObject.Keys) { | |
$value = "{0}" -f $InputObject.Item($key).ToString() | |
if (-not $KeepCase) {$value = $value.ToLower()} | |
$output.Add("$KeyPrefix$($key)$ValuePrefix`"$($value)`"") | |
} | |
$output.Add("}") | |
$output.ToArray() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment