function ConvertTo-PowerShellDataFileText { [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [AllowNull()] $InputObject, [Parameter(Mandatory=$false)] [int] $Indent = 0 ) $IndentText = " " * $Indent $ChildIndent = $Indent + 4 $ChildIndentText = " " * $ChildIndent if($null -eq $InputObject){ return '$null' } if($InputObject -is [bool]){ if($InputObject){ return '$true' } return '$false' } if($InputObject -is [int] -or $InputObject -is [long] -or $InputObject -is [decimal] -or $InputObject -is [double]){ return ([string]$InputObject) } if($InputObject -is [string]){ return "'$($InputObject.Replace("'", "''"))'" } if($InputObject -is [System.Collections.IDictionary]){ $Lines = @("@{") foreach($Key in $InputObject.Keys){ $KeyText = "'$(([string]$Key).Replace("'", "''"))'" $ValueText = ConvertTo-PowerShellDataFileText -InputObject $InputObject[$Key] -Indent $ChildIndent $Lines += "$ChildIndentText$KeyText = $ValueText" } $Lines += "$IndentText}" return ($Lines -join [Environment]::NewLine) } if($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]){ $Items = @($InputObject) if($Items.Count -eq 0){ return '@()' } $Lines = @("@(") foreach($Item in $Items){ $ValueText = ConvertTo-PowerShellDataFileText -InputObject $Item -Indent $ChildIndent $Lines += "$ChildIndentText$ValueText" } $Lines += "$IndentText)" return ($Lines -join [Environment]::NewLine) } return "'$(([string]$InputObject).Replace("'", "''"))'" }