How to Pass Configuration Data to a Node Configuration in #PSDSC
Published on 29 Dec 2014Tags #Desired State Configuration #DSC #PowerShell #PSDSC #Splatting
You are probably using a regular PowerShell script to compile your node configurations into MOF files. If you are using configuration data instead of parameters, you need to pull it into your script to pass it on to the node configuration. There are two very different approaches to this.
Using a data file
If you decide to store your configuration data in a PowerShell data file (*.psd1
), you can easily pass it on to your node configuration using splatting. See the @Params
variable in the following example and note that ConfigurationData
requires a file object:
$DataFile = Join-Path $PSScriptRoot "ConfigurationData.psd1"
$ConfigFile = Join-Path $PSScriptRoot "Configuration.psm1"
Import-Module $ConfigFile
$Params = @(OutputPath $OutputPath; ConfigurationData $DataFile)
MyConfiguration @Params
Note that the contents of you ConfigurationData.psd1
must look like the following:
@{
AllNodes = @(
<# ... #>
)
}
Validating Complex Config Data
If your configuration data increases in complexity you may want to run validation checks before passing it to your node configuration. Unfortunately, the above example does not give you access to the configuration data before it is consumed by your node configuration.
To make this even worse, splatting does not work because it expects a file object but is actually getting a hash table. The only way to access the data structure is by placing the configuration data in a regular PowerShell script file (*.ps1
) and dot-sourcing it:
$DataFile = Join-Path $PSScriptRoot "ConfigurationData.ps1"
$ConfigFile = Join-Path $PSScriptRoot "Configuration.psm1"
. $DataFile
Import-Module $ConfigFile
MyConfiguration -OutputPath $OutputPath -ConfigurationData $ConfigData
In this example you need to make sure that ConfigurationData.ps1
contains a data structure called $ConfigData
:
$ConfigData = @{
AllNodes = @(
<# ... #>
)
}
Please also refer to my post about designing node configurations.