152 lines
4.6 KiB
PowerShell
152 lines
4.6 KiB
PowerShell
function Export-Hashtable {
|
|
<#
|
|
.SYNOPSIS
|
|
Exportiert einen PowerShell Hashtable als valide PSD1-Datei.
|
|
|
|
.DESCRIPTION
|
|
Diese Funktion konvertiert einen PowerShell Hashtable in eine gültige PSD1-Datei
|
|
mit korrekter Formatierung und Einrückung.
|
|
|
|
.PARAMETER Hashtable
|
|
Der zu exportierende Hashtable
|
|
|
|
.PARAMETER Path
|
|
Der Pfad zur Ziel-PSD1-Datei
|
|
|
|
.PARAMETER Indent
|
|
Die Anzahl der Leerzeichen für die Einrückung (Standard: 4)
|
|
|
|
.EXAMPLE
|
|
$data = @{
|
|
ModuleName = "MeinModul"
|
|
Version = "1.0.0"
|
|
Author = "Max Mustermann"
|
|
Settings = @{
|
|
Debug = $true
|
|
MaxRetries = 3
|
|
}
|
|
}
|
|
Export-Hashtable -Hashtable $data -Path "C:\config.psd1"
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[System.Collections.Hashtable]$Hashtable,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[System.String]$Path,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[Int]$Indent = 4
|
|
)
|
|
|
|
function ConvertTo-Psd1String {
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
$Object,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[int]$Level = 0
|
|
)
|
|
|
|
$indentStr = " " * ($Indent * $Level)
|
|
$nextIndentStr = " " * ($Indent * ($Level + 1))
|
|
|
|
if ($null -eq $Object) {
|
|
return "`$null"
|
|
}
|
|
|
|
switch ($Object.GetType().Name) {
|
|
"Hashtable" {
|
|
if ($Object.Count -eq 0) {
|
|
return "@{}"
|
|
}
|
|
|
|
$lines = @("@{")
|
|
foreach ($key in ($Object.Keys)) {
|
|
$value = ConvertTo-Psd1String -Object $Object[$key] -Level ($Level + 1)
|
|
$lines += "$nextIndentStr$key = $value"
|
|
}
|
|
$lines += "$indentStr}"
|
|
return ($lines -join "`r`n")
|
|
}
|
|
|
|
"Object[]" {
|
|
if ($Object.Count -eq 0) {
|
|
return "@()"
|
|
}
|
|
|
|
$lines = @("@(")
|
|
foreach ($item in $Object) {
|
|
$value = ConvertTo-Psd1String -Object $item -Level ($Level + 1)
|
|
$lines += "$nextIndentStr$value"
|
|
}
|
|
$lines += "$indentStr)"
|
|
return ($lines -join "`r`n")
|
|
}
|
|
|
|
"ArrayList" {
|
|
return ConvertTo-Psd1String -Object $Object.ToArray() -Level $Level
|
|
}
|
|
|
|
"String" {
|
|
# Escape für einfache Anführungszeichen
|
|
$escaped = $Object -replace "'", "''"
|
|
return "'$escaped'"
|
|
}
|
|
|
|
"Boolean" {
|
|
return "`$$Object"
|
|
}
|
|
|
|
"Int32" {
|
|
return $Object.ToString()
|
|
}
|
|
|
|
"Int64" {
|
|
return $Object.ToString()
|
|
}
|
|
|
|
"Double" {
|
|
return $Object.ToString()
|
|
}
|
|
|
|
default {
|
|
if ($Object -is [Array]) {
|
|
return ConvertTo-Psd1String -Object @($Object) -Level $Level
|
|
}
|
|
# Fallback für andere Typen
|
|
return "'$Object'"
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
$psd1Content = ConvertTo-Psd1String -Object $Hashtable
|
|
|
|
# Stelle sicher, dass das Verzeichnis existiert
|
|
$directory = Split-Path -Path $Path -Parent
|
|
if ($directory -and -not (Test-Path -Path $directory)) {
|
|
New-Item -Path $directory -ItemType Directory -Force | Out-Null
|
|
}
|
|
|
|
# Schreibe die Datei mit UTF8-BOM Encoding (Standard für PSD1)
|
|
$psd1Content | Out-File -FilePath $Path -Encoding utf8 -Force
|
|
|
|
Write-Verbose "Hashtable erfolgreich nach '$Path' exportiert"
|
|
|
|
# Validiere die erstellte PSD1-Datei
|
|
try {
|
|
$null = Import-PowerShellDataFile -Path $Path
|
|
Write-Verbose "PSD1-Datei erfolgreich validiert"
|
|
}
|
|
catch {
|
|
Write-Warning "Die erstellte PSD1-Datei konnte nicht validiert werden: $_"
|
|
}
|
|
}
|
|
catch {
|
|
Write-Error "Fehler beim Exportieren des Hashtables: $_"
|
|
throw
|
|
}
|
|
} |