Update module version to 1.0.1 and add new functions for exporting and deploying DSC configuration data

This commit is contained in:
Torsten Brendgen
2026-07-03 11:59:18 +02:00
parent fcbe61902f
commit fb9209456b
9 changed files with 491 additions and 8 deletions

View File

@@ -0,0 +1,29 @@
function ConvertTo-ConfigurationDataRelativePath {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]
$Path,
[Parameter(Mandatory=$true)]
[string]
$BasePath
)
$ResolvedPath = Repair-ConfigurationDataPathEncoding -Path $Path
$ResolvedBasePath = Repair-ConfigurationDataPathEncoding -Path $BasePath
try {
$PathUri = [System.Uri]::new((Resolve-Path -LiteralPath $ResolvedPath).ProviderPath)
$BaseUriPath = [System.IO.Path]::GetFullPath($ResolvedBasePath)
if(-not $BaseUriPath.EndsWith([System.IO.Path]::DirectorySeparatorChar)){
$BaseUriPath += [System.IO.Path]::DirectorySeparatorChar
}
$BaseUri = [System.Uri]::new($BaseUriPath)
return [System.Uri]::UnescapeDataString($BaseUri.MakeRelativeUri($PathUri).ToString()).Replace('/', [System.IO.Path]::DirectorySeparatorChar)
}
catch {
return $ResolvedPath
}
}

View File

@@ -0,0 +1,64 @@
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("'", "''"))'"
}

View File

@@ -0,0 +1,21 @@
function Repair-ConfigurationDataPathEncoding {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]
$Path
)
if($Path -notmatch "[ÃÂ]"){
return $Path
}
try {
$Bytes = [System.Text.Encoding]::GetEncoding(1252).GetBytes($Path)
return [System.Text.Encoding]::UTF8.GetString($Bytes)
}
catch {
return $Path
}
}

View File

@@ -10,12 +10,31 @@ function Resolve-ConfigurationDataTemplatePath {
$BasePath = (Get-Location).Path
)
$CandidatePath = $Path
if(-not [System.IO.Path]::IsPathRooted($CandidatePath)){
$CandidatePath = Join-Path -Path $BasePath -ChildPath $CandidatePath
$CandidatePaths = @()
foreach($CurrentPath in @($Path, (Repair-ConfigurationDataPathEncoding -Path $Path))){
if([string]::IsNullOrWhiteSpace($CurrentPath)){
continue
}
$CandidatePath = $CurrentPath
$CandidateBasePath = Repair-ConfigurationDataPathEncoding -Path $BasePath
if(-not [System.IO.Path]::IsPathRooted($CandidatePath)){
$CandidatePath = Join-Path -Path $CandidateBasePath -ChildPath $CandidatePath
}
if($CandidatePaths -notcontains $CandidatePath){
$CandidatePaths += $CandidatePath
}
}
$ResolvedPath = $null
foreach($CandidatePath in $CandidatePaths){
$ResolvedPath = Resolve-Path -LiteralPath $CandidatePath -ErrorAction SilentlyContinue
if($null -ne $ResolvedPath){
break
}
}
$ResolvedPath = Resolve-Path -Path $CandidatePath -ErrorAction SilentlyContinue
if($null -eq $ResolvedPath){
throw "Configuration data template [$Path] was not found. Base path [$BasePath]."
}