Skip to content

Instantly share code, notes, and snippets.

@jschpp
Last active December 29, 2022 18:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschpp/aaaa930318cde71c867c07b76c13bccf to your computer and use it in GitHub Desktop.
Save jschpp/aaaa930318cde71c867c07b76c13bccf to your computer and use it in GitHub Desktop.
Exports a powershell object to a psd1 format. Can be re-imported via Import-PowershellDataFile
function Get-IdentedString {
<#
.SYNOPSIS
Returns $obj as indented string
.DESCRIPTION
Turns $obj to string and adds $indent * 4 spaces in front of it
.PARAMETER obj
Object to be converted to String
.PARAMETER indent
How many whitespaces * 4 to indent
.EXAMPLE
Get-IdentedString "Test" 1
>>> " Test"
#>
param (
$obj,
[int]$indent = 0
)
return "{0,$($indent *4)}{1}" -f "", $obj
}
function Convert-Array ($obj, [int]$indent = 0, [switch]$SkipIndentOnce) {
if ($SkipIndentOnce) {
$out += Get-IdentedString "@(`n" -indent 0
}
else {
$out = Get-IdentedString "@(`n" -indent $indent
}
foreach ($elem in $obj) {
$out += Convert-Element -obj $elem -indent $($indent + 1)
$out += "`n"
}
$out += Get-IdentedString ")" -indent $indent
return $out
}
function Convert-Int ($obj, [int]$indent = 0, [switch]$SkipIndentOnce) {
if ($SkipIndentOnce) {
return Get-IdentedString "$obj" -indent 0
}
else {
return Get-IdentedString "$obj" -indent $indent
}
}
function Convert-String ($obj, [int]$indent = 0, [switch]$SkipIndentOnce) {
if ($SkipIndentOnce) {
return Get-IdentedString "`"$obj`"" -indent 0
}
else {
return Get-IdentedString "`"$obj`"" -indent $indent
}
}
function Convert-Element {
param (
$obj,
[int]$indent = 0,
[switch]$SkipIndentOnce
)
$paramObj = @{
obj = $obj
indent = $indent
}
if ($SkipIndentOnce) {
$paramObj["SkipIndentOnce"] = $true
}
switch -Regex ($obj.GetType().Name) {
"Object\[\]" {return Convert-Array @paramObj}
"Hashtable" {return Convert-Hashtable @paramObj}
'Int\d+' {return Convert-Int @paramObj}
default {return Convert-String @paramObj}
}
}
function Convert-Hashtable ($obj, $indent = 0, [switch]$SkipIndentOnce) {
if ($SkipIndentOnce) {
return Get-IdentedString "@{`n" -indent 0
}
else {
$out = Get-IdentedString "@{`n" -indent $indent
}
$indent += 1
foreach ($key in $obj.Keys) {
$out += Convert-Element -obj $key -indent $indent
$out += " = "
$out += Convert-Element -obj $obj[$key] -indent $indent -SkipIndentOnce
$out += "`n"
}
$indent -= 1
$out += Get-IdentedString "}`n" -indent $indent
return $out
}
function Export-PowerShellDataFile {
param (
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)
]$Obj,
$Path,
[switch]$NoClobber
)
if ($Path) {
if ($NoClobber -and $(Test-Path $Path)) {
Write-Error "File exists" -ErrorAction Stop
}
Convert-Element -obj $Obj | Out-File $Path
}
else {
Convert-Element -obj $Obj | Write-Output
}
}
Export-ModuleMember Export-PowerShellDataFile
@lboening
Copy link

I attempted to pass this along: $obj = [PsCustomObject]@{Name='file'}. It could not process that object. I tried with a hash table and it worked. I am recasting it a bit into a different module. Instead of using Export-ModuleMember, I put it in the "public" subfolder of my module apparatus. I then dot then entire script as a function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment