PoshCode Logo PowerShell Code Repository

ConvertToStringData by Chad Miller 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1986"></script>download | new post

Converts a hashtable to a string representation of the hashtable definition. Useful in persisting hashtables to .NET Isolated Storage

  1. function ConvertTo-StringData
  2. {
  3.     Begin
  4.     {
  5.        $string  = "@{`n"
  6.         function Expand-Value
  7.         {
  8.             param($value)
  9.  
  10.             if ($value -ne $null) {
  11.                 switch ($value.GetType().Name)
  12.                 {
  13.                     'String' { "`"$value`"" }
  14.                     'Boolean' { "`$$value" }
  15.                     default { $value }
  16.                 }
  17.             }
  18.             else
  19.             { "`$null" }
  20.  
  21.         }
  22.     }
  23.     Process
  24.     {
  25.         $string += $_.GetEnumerator() | foreach {"{0} = {1}`n" -f $_.Name,(Expand-Value $_.Value)}
  26.     }
  27.     End
  28.     {
  29.         $string += "}"
  30.         Write-Output $string
  31.     }
  32.  
  33. } #ConvertTo-StringData

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me