# 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