Functions/Out-HashString.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
function Out-HashString { <# .SYNOPSIS Convert an hashtable or and OrderedDictionary to a string .DESCRIPTION Convert an hashtable or and OrderedDictionary to a string .PARAMETER InputObject The object that is to be converted .PARAMETER PreSpacing Number of spaces used for indentation .EXAMPLE $hashObject = @{ Name = "Tore" Goal = "Rule the World" } $hashObject | Out-HashString This will convert the hashtable to the following string @{ Name = "Tore" Goal = "Rule the World" } .INPUTS Hashtable .OUTPUTS string .NOTES Author: Tore Groneng Website: www.firstpoint.no Twitter: @ToreGroneng #> [cmdletbinding()] Param( [Parameter(ValueFromPipeLine)] $InputObject , [string]$PreSpacing = " " ) Begin { $f = $MyInvocation.InvocationName Write-Verbose -Message "$f - START" $newLine = [environment]::NewLine if ($PreSpacing) { $endspaceCount = $PreSpacing.Length - 4 if ($endspaceCount -lt 0){$endspaceCount = 0} $endspace = " " * $endspaceCount $beginSpace = " " * $endspaceCount } } Process { $out = "@{" $out = $out + $newLine $preSpace = $PreSpacing if (-not $InputObject -or $InputObject.keys.count -eq 0) {return "@{}"} foreach ($key in $InputObject.Keys) { Write-Verbose -Message "$f - Processing key [$key]" if ($key.Contains('$')) { $DisplayKey = "'$key'" } $value = $InputObject.$key $objType = $value.GetType().Name Write-Verbose -Message "$f - ObjectType = $objType" $mode = "stringValue" if ($objType -eq "Hashtable" -or $objType -eq "OrderedDictionary") { $mode = "hashtable" } if ($value -is [array]) { if ($value[0]) { $arrayType = $value[0].GetType().Name Write-Verbose -Message "$f - arrayType is [$arrayType]" if ($arrayType -eq "Hashtable" -or $arrayType -eq "OrderedDictionary") { $mode = "HashTableValue" } else { $mode = "ArrayValue" } } } Write-Verbose -Message "$f - Mode is [$mode]" if ($DisplayKey) { $key = $DisplayKey $DisplayKey = $null } $out += "$PreSpacing$key = " switch ($mode) { 'stringValue' { $out += '"' + $value + '"' + $newLine } 'hashtable' { $stringValue = Out-HashString -InputObject $value -PreSpacing "$PreSpacing " $out += $stringValue + $newLine } 'HashTableValue' { $stringValue = "" foreach ($arrayHash in $value) { $hashString = Out-HashString -InputObject $arrayHash -PreSpacing "$PreSpacing " $hash = " $hashString" $hash = "$hash," + $newLine $stringValue += $hash } $separatorIndex = $stringValue.LastIndexOf(",") $stringValue = $stringValue.Remove($separatorIndex,1) $out += $stringValue } 'ArrayValue' { $out += '"' +($value -join '","') + '"' + $newLine } Default {} } } $out += "$endspace}" $out } } |