PoshCode Archive  Artifact [c98d281ac5]

Artifact c98d281ac56013d0e8aff6e67f9e94c42f7fc3b17d84308b7968ea669a329b5d:

  • File ConvertToStringData.ps1 — part of check-in [4f7d645d03] at 2018-06-10 13:03:41 on branch trunk — Converts a hashtable to a string representation of the hashtable definition. Useful in persisting hashtables to .NET Isolated Storage (user: Chad Miller size: 1150)

# encoding: ascii
# api: powershell
# title: ConvertToStringData
# description: Converts a hashtable to a string representation of the hashtable definition. Useful in persisting hashtables to .NET Isolated Storage
# version: 0.1
# type: function
# author: Chad Miller
# license: CC0
# function: ConvertTo-StringData
# x-poshcode-id: 1986
# x-archived: 2015-06-27T05:15:32
# x-published: 2011-07-19T19:27:00
#
#
function ConvertTo-StringData
{ 
    Begin 
    { 
       $string  = "@{`n"
        function Expand-Value
        {
            param($value)

            if ($value -ne $null) {
                switch ($value.GetType().Name)
                {
                    'String' { "`"$value`"" }
                    'Boolean' { "`$$value" }
                    default { $value }
                }
            }
            else
            { "`$null" }

        }
    } 
    Process 
    { 
        $string += $_.GetEnumerator() | foreach {"{0} = {1}`n" -f $_.Name,(Expand-Value $_.Value)}
    } 
    End 
    { 
        $string += "}" 
        Write-Output $string
    }

} #ConvertTo-StringData