Refactor configuration data export and template loading functions

- Updated Export-ConfigurationData to use resolved configuration data directly and added error handling for missing data.
- Enhanced Load-Templates to streamline category loading and improve error handling for missing template directories.
- Removed Merge-ConfigurationData function as it is no longer needed.
- Refactored Merge-Templates to improve merging logic and handle default files more effectively.
- Introduced Get-TemplatePreviewData function to handle template preview data retrieval and merging.
- Added Import-OrderedPowerShellDataFile function to support ordered hashtable imports.
- Updated Update-ParametersPanel to handle parameter and variable tab updates more efficiently.
- Improved Update-TreeView to handle cases where resources are not found.
- Added new parameters to Environment.Default.psd1 for Active Directory configuration.
- Created new template BGW-LAN.psd1 for domain configuration.
This commit is contained in:
Torsten Brendgen
2026-06-29 22:16:05 +02:00
parent 6986570510
commit 1a4ba96dc8
13 changed files with 689 additions and 4456 deletions

View File

@@ -1,126 +1,172 @@
function Load-Templates {
$flow.SuspendLayout()
$flow.Controls.Clear()
# Lokales Tracking-Objekt
$selectionTracker = @{
LastSelectedListBox = $null
}
if (-not (Test-Path -Path $($settingsManager.Get("TemplatePath")))) {
$statusBar.SetText("Status", "Root-Ordner nicht gefunden: $rootPath")
$templatePath = $settingsManager.Get("TemplatePath")
if (-not (Test-Path -Path $templatePath)) {
$statusBar.SetText("Status", "Template-Ordner nicht gefunden: $templatePath")
$flow.ResumeLayout()
return
}
else {
try {
$Categories = Get-ChildItem -Path $($settingsManager.Get("TemplatePath")) -Directory -ErrorAction Stop
}
catch {
$statusBar.SetText("Status", "Fehler beim Laden: $($_.Exception.Message)")
[System.Windows.Forms.MessageBox]::Show(
"Konnte Kategorien nicht laden: $($_.Exception.Message)",
"Fehler",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
return
}
if ($Categories.Count -eq 0) {
$labelNoCategories = (
[LabelBuilder]::new("Keine Unterordner gefunden.", 24).
SetDock('Top').
SetTextAlign([System.Drawing.ContentAlignment]::MiddleLeft).
SetPadding(5, 0, 0, 0).
Build()
)
$flow.Controls.Add($labelNoCategories)
return
}
else {
foreach ($Categorie in $Categories) {
$GroupBox = (
[GroupBoxBuilder]::new($Categorie.Name).
#SetAutoSize($true).
SetWidth(0).
SetHeight(90).
SetMargin(4).
SetPadding(10).
AddTo($flow)
)
$GroupBox.Tag = $Categorie.Name
$Files = Get-ChildItem -Path $Categorie.FullName -Filter "*.psd1" -File -ErrorAction SilentlyContinue | Sort-Object Name
if ($Files.Count -eq 0) {
$ListBox = (
[ListBoxBuilder]::new().
SetDock('Fill'). # Füllt die GroupBox aus
AddItem("Keine Templates").
AddTo($GroupBox)
)
try {
$categories = @(Get-ChildItem -Path $templatePath -Directory -ErrorAction Stop | Where-Object {
@(Get-ChildItem -Path $_.FullName -Filter "*.psd1" -File -ErrorAction SilentlyContinue).Count -gt 0
})
}
catch {
$statusBar.SetText("Status", "Fehler beim Laden: $($_.Exception.Message)")
[System.Windows.Forms.MessageBox]::Show(
"Konnte Kategorien nicht laden: $($_.Exception.Message)",
"Fehler",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
$flow.ResumeLayout()
return
}
if ($categories.Count -eq 0) {
$labelNoCategories = (
[LabelBuilder]::new("Keine Templates gefunden.", 24).
SetDock('Top').
SetTextAlign([System.Drawing.ContentAlignment]::MiddleLeft).
SetPadding(5, 0, 0, 0).
Build()
)
$flow.Controls.Add($labelNoCategories)
$flow.ResumeLayout()
return
}
foreach ($category in $categories) {
$groupBoxWidth = [Math]::Max(($flow.ClientSize.Width - 20), 220)
$groupBox = (
[GroupBoxBuilder]::new($category.Name).
SetWidth($groupBoxWidth).
SetHeight(90).
SetMargin(4).
SetPadding(10).
AddTo($flow)
)
$groupBox.Tag = $category.Name
$files = @(Get-ChildItem -Path $category.FullName -Filter "*.psd1" -File -ErrorAction SilentlyContinue |
Where-Object { $_.BaseName -ne "Default" -and $_.BaseName -notlike "*.Default" } |
Sort-Object Name)
$listBox = (
[ListBoxBuilder]::new().
SetDock('Fill').
AddSelectedIndexChangedHandler({
param($s, $e)
if ($null -ne $selectionTracker.LastSelectedListBox -and $selectionTracker.LastSelectedListBox -ne $s) {
$selectionTracker.LastSelectedListBox.ClearSelected()
}
else {
$ListBox = (
[ListBoxBuilder]::new().
SetDock('Fill').
AddSelectedIndexChangedHandler({
param($s, $e)
if ($null -ne $selectionTracker.LastSelectedListBox -and
$selectionTracker.LastSelectedListBox -ne $s) {
# Deselektiere die vorherige ListBox
$selectionTracker.LastSelectedListBox.ClearSelected()
}
# Speichere die aktuelle ListBox
$selectionTracker.LastSelectedListBox = $s
$selectionTracker.LastSelectedListBox = $s
$selectedItem = $s.SelectedItem
$selectedItem = $s.SelectedItem
if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) {
$currentGroupBox = $s.Parent
$statusBar.SetText("Status", "Ausgewählt: $($selectedItem.FullName) [Kategorie: $($currentGroupBox.Tag)]")
Show-Template -Template $([TemplateBuilder]::new($selectedItem.FullName).ResolveDeploymentDataVariables())
}
}.GetNewClosure()).
AddMouseDoubleClickHandler({
param($s, $e)
$selectedItem = $s.SelectedItem
if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) {
$fullPath = $selectedItem.FullName
if (-not (Test-Path $fullPath)) {
[System.Windows.Forms.MessageBox]::Show("Datei nicht gefunden: $fullPath", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$newItem = New-Object PSObject -Property @{ BaseName = [System.IO.Path]::GetFileNameWithoutExtension($fullPath); FullName = $fullPath }
if (-not ($selectionList.Items | Where-Object { $_.FullName -eq $newItem.FullName })) {
$selectionList.Items.Add($newItem) | Out-Null
$stateManager.SetState("hasSelection", $true)
$stateManager.SetState("isMerged", $false)
$stateManager.UpdateAllButtons()
$statusBar.SetText("Status", "Zur Auswahl hinzugefügt: $fullPath")
}
else {
$statusBar.SetText("Status", "Datei bereits in der Auswahl")
}
}
})
)
if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) {
$currentGroupBox = $s.Parent
$statusBar.SetText("Status", "Ausgewählt: $($selectedItem.FullName) [Kategorie: $($currentGroupBox.Tag)]")
foreach ($file in $files) {
$item = New-Object PSObject -Property @{ BaseName = $file.BaseName; FullName = $file.FullName }
$ListBox.AddItem($item)
$ListBox.AddTo($GroupBox)
try {
if (-not (Get-Command -Name Get-TemplatePreviewData -ErrorAction SilentlyContinue)) {
$previewFunctionPath = Join-Path -Path $settingsManager.Get("FunctionsPath") -ChildPath "Get-TemplatePreviewData.ps1"
if (Test-Path -Path $previewFunctionPath -PathType Leaf) {
. $previewFunctionPath
}
}
if (-not (Get-Command -Name Get-TemplatePreviewData -ErrorAction SilentlyContinue)) {
$templateData = Import-OrderedPowerShellDataFile -Path $selectedItem.FullName -ErrorAction Stop
Show-Template -Template $templateData
$statusBar.SetText("Status", "Vorschau ohne Default-Merge geladen: Get-TemplatePreviewData ist nicht verfügbar")
return
}
$preview = Get-TemplatePreviewData -TemplatePath $selectedItem.FullName -ErrorAction Stop
$previewData = if ($null -ne $preview.Display) { $preview.Display } elseif ($null -ne $preview.Resolved) { $preview.Resolved } else { $preview.Merged }
Show-Template -Template $previewData
Update-ParametersPanel -ConfigurationData $preview.Merged
if ($null -ne $preview.ResolveError) {
$statusBar.SetText("Status", "Vorschau gemerged, Resolve unvollständig: $($preview.ResolveError.Exception.Message)")
}
else {
$statusBar.SetText("Status", "Vorschau geladen: $($preview.Files.Count) Datei(en) inklusive Defaults")
}
}
catch {
[System.Windows.Forms.MessageBox]::Show(
"Datei konnte nicht geladen werden: $($_.Exception.Message)",
"Fehler",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
}
}
}.GetNewClosure()).
AddMouseDoubleClickHandler({
param($s, $e)
$selectedItem = $s.SelectedItem
if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) {
$fullPath = $selectedItem.FullName
if (-not (Test-Path $fullPath)) {
[System.Windows.Forms.MessageBox]::Show("Datei nicht gefunden: $fullPath", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
return
}
$newItem = New-Object PSObject -Property @{
BaseName = [System.IO.Path]::GetFileNameWithoutExtension($fullPath)
FullName = $fullPath
}
if (-not ($selectionList.Items | Where-Object { $_.FullName -eq $newItem.FullName })) {
$selectionList.Items.Add($newItem) | Out-Null
$stateManager.SetState("hasSelection", $true)
$stateManager.SetState("isMerged", $false)
$stateManager.UpdateAllButtons()
$statusBar.SetText("Status", "Zur Auswahl hinzugefügt: $fullPath")
}
else {
$statusBar.SetText("Status", "Datei bereits in der Auswahl")
}
}
}).
AddTo($groupBox)
)
if($files.Count -eq 0){
$listBox.Items.Add("Keine Templates vorhanden") | Out-Null
}else{
foreach ($file in $files) {
$item = New-Object PSObject -Property @{
BaseName = $file.BaseName
FullName = $file.FullName
}
$listBox.Items.Add($item) | Out-Null
}
$flow.ResumeLayout()
}
}
$flow.ResumeLayout()
foreach ($groupBox in $flow.Controls) {
$groupBox.Width = [Math]::Max(($flow.ClientSize.Width - 20), 220)
}
$form.Form.Invoke([Action] {
$splitPanel.AutoSizeSplitterToPanel1Content()
})
}
$splitPanel.AutoSizeSplitterToPanel1Content()
})
}