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:
117
Functions/Import-OrderedPowerShellDataFile.ps1
Normal file
117
Functions/Import-OrderedPowerShellDataFile.ps1
Normal file
@@ -0,0 +1,117 @@
|
||||
function Import-OrderedPowerShellDataFile {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Path
|
||||
)
|
||||
|
||||
function ConvertTo-OrderedHashtable {
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
$HashtableAst
|
||||
)
|
||||
|
||||
$Ordered = New-Object System.Collections.Specialized.OrderedDictionary
|
||||
$SortedPairs = $HashtableAst.KeyValuePairs | Sort-Object { $_.Item1.Extent.StartOffset }
|
||||
|
||||
foreach($Pair in $SortedPairs){
|
||||
$Key = $Pair.Item1.Extent.Text.Trim("'`"")
|
||||
$Ordered.Add($Key, (ConvertFrom-DataAst -Ast $Pair.Item2))
|
||||
}
|
||||
|
||||
return $Ordered
|
||||
}
|
||||
|
||||
function ConvertFrom-DataAst {
|
||||
Param(
|
||||
[AllowNull()]
|
||||
$Ast
|
||||
)
|
||||
|
||||
if($null -eq $Ast){
|
||||
return $null
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.HashtableAst]){
|
||||
return ConvertTo-OrderedHashtable -HashtableAst $Ast
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.PipelineAst]){
|
||||
return ConvertFrom-DataAst -Ast $Ast.PipelineElements[0]
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.CommandExpressionAst]){
|
||||
return ConvertFrom-DataAst -Ast $Ast.Expression
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.ParenExpressionAst]){
|
||||
return ConvertFrom-DataAst -Ast $Ast.Pipeline
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.StatementBlockAst]){
|
||||
$Items = @()
|
||||
foreach($Statement in $Ast.Statements){
|
||||
$Items += ,(ConvertFrom-DataAst -Ast $Statement)
|
||||
}
|
||||
return ,$Items
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.ArrayExpressionAst]){
|
||||
return @(ConvertFrom-DataAst -Ast $Ast.SubExpression)
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.ArrayLiteralAst]){
|
||||
$Items = @()
|
||||
foreach($Element in $Ast.Elements){
|
||||
$Items += ,(ConvertFrom-DataAst -Ast $Element)
|
||||
}
|
||||
return ,$Items
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.StringConstantExpressionAst]){
|
||||
return $Ast.Value
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.ExpandableStringExpressionAst]){
|
||||
return $Ast.Value
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.ConstantExpressionAst]){
|
||||
return $Ast.Value
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.VariableExpressionAst]){
|
||||
switch($Ast.VariablePath.UserPath){
|
||||
"true" { return $true }
|
||||
"false" { return $false }
|
||||
"null" { return $null }
|
||||
}
|
||||
}
|
||||
|
||||
if($Ast -is [System.Management.Automation.Language.UnaryExpressionAst]){
|
||||
$ChildValue = ConvertFrom-DataAst -Ast $Ast.Child
|
||||
if($Ast.TokenKind -eq [System.Management.Automation.Language.TokenKind]::Minus){
|
||||
return -1 * $ChildValue
|
||||
}
|
||||
return $ChildValue
|
||||
}
|
||||
|
||||
throw "Nicht unterstützter Ausdruck in PSD1: $($Ast.Extent.Text)"
|
||||
}
|
||||
|
||||
$Tokens = $null
|
||||
$Errors = $null
|
||||
$Ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$Tokens, [ref]$Errors)
|
||||
|
||||
if($Errors.Count -gt 0){
|
||||
throw "Fehler beim Parsen der Datei: $($Errors[0].Message)"
|
||||
}
|
||||
|
||||
$HashtableAst = $Ast.Find({ $args[0] -is [System.Management.Automation.Language.HashtableAst] }, $true)
|
||||
if($null -eq $HashtableAst){
|
||||
throw "Keine Hashtable in der Datei gefunden."
|
||||
}
|
||||
|
||||
return ConvertTo-OrderedHashtable -HashtableAst $HashtableAst
|
||||
}
|
||||
Reference in New Issue
Block a user