Files
Merge-DSCConfigurationData/Public/New-DSCConfigurationDataDeployment.ps1

140 lines
4.2 KiB
PowerShell

function New-DSCConfigurationDataDeployment {
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[Parameter(Mandatory=$false)]
[string]
$Path,
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter(Mandatory=$true)]
[string]
$DeploymentId,
[Parameter(Mandatory=$true)]
[Alias("SourceTemplates")]
[string[]]
$SourceTemplatePath,
[Parameter(Mandatory=$false)]
[hashtable[]]
$AllNodes = @(),
[Parameter(Mandatory=$false)]
[System.Collections.IDictionary]
$DeploymentData = @{},
[Parameter(Mandatory=$false)]
[switch]
$UseRelativePaths,
[Parameter(Mandatory=$false)]
[switch]
$Export,
[Parameter(Mandatory=$false)]
[ValidateSet("PowerShellDataFile", "Json")]
[string]
$Format = "PowerShellDataFile",
[Parameter(Mandatory=$false)]
[switch]
$Force,
[Parameter(Mandatory=$false)]
[switch]
$PassThru
)
if($Export -and [string]::IsNullOrWhiteSpace($Path)){
throw "Parameter [Path] is required when [Export] is used."
}
$ResolvedOutputPath = $null
if(-not [string]::IsNullOrWhiteSpace($Path)){
$ResolvedOutputPath = Repair-ConfigurationDataPathEncoding -Path $Path
}
$OutputDirectory = if([string]::IsNullOrWhiteSpace($ResolvedOutputPath)){
(Get-Location).Path
}else{
Split-Path -Path $ResolvedOutputPath -Parent
}
if([string]::IsNullOrWhiteSpace($OutputDirectory)){
$OutputDirectory = (Get-Location).Path
}
$MergeResult = Merge-DSCConfigurationData -Path $SourceTemplatePath -PassThru
$MergedTemplateData = $MergeResult.ConfigurationData
$DeploymentOverride = Copy-ConfigurationDataValue -Value $DeploymentData
if($AllNodes.Count -gt 0){
if(-not (Test-ConfigurationDataDictionaryKey -Dictionary $DeploymentOverride -Key "Resources")){
$DeploymentOverride["Resources"] = @{}
}
$DeploymentOverride["Resources"]["AllNodes"] = $AllNodes
}
$ConfigurationData = Merge-DSCConfigurationData -Template $MergedTemplateData -Deployment $DeploymentOverride
$TemplatePaths = @($SourceTemplatePath | ForEach-Object {
$Resolved = Resolve-ConfigurationDataTemplatePath -Path $_
if($UseRelativePaths){
ConvertTo-ConfigurationDataRelativePath -Path $Resolved -BasePath $OutputDirectory
}else{
$Resolved
}
})
$SourceFiles = @($MergeResult.Files | ForEach-Object {
Get-ConfigurationDataSourceFileInfo -Path $_ -BasePath $OutputDirectory -UseRelativePath:$UseRelativePaths
})
$ConfigurationData["Metadata"] = [ordered]@{
TemplateType = "Deployment"
Name = $Name
DeploymentId = $DeploymentId
GeneratedOn = (Get-Date).ToString("s")
Sources = [ordered]@{
Templates = $TemplatePaths
Files = $SourceFiles
}
}
if(-not $Export){
return $ConfigurationData
}
if($PSCmdlet.ShouldProcess($ResolvedOutputPath, "Export DSC configuration data deployment as [$Format]")){
switch($Format){
"PowerShellDataFile" {
Export-PowerShellDataFile -InputObject $ConfigurationData -Path $ResolvedOutputPath -Force:$Force
}
"Json" {
if((Test-Path -LiteralPath $ResolvedOutputPath -PathType Leaf) -and (-not $Force)){
throw "File [$ResolvedOutputPath] already exists. Use -Force to overwrite it."
}
$Parent = Split-Path -Path $ResolvedOutputPath -Parent
if(-not [string]::IsNullOrWhiteSpace($Parent) -and -not (Test-Path -LiteralPath $Parent)){
New-Item -Path $Parent -ItemType Directory -Force | Out-Null
}
$ConfigurationData | ConvertTo-Json -Depth 100 | Set-Content -LiteralPath $ResolvedOutputPath -Encoding UTF8
}
}
}
if($PassThru){
return [PSCustomObject]@{
Path = $ResolvedOutputPath
Files = $MergeResult.Files
ConfigurationData = $ConfigurationData
}
}
}