As with functions, you can create configuration blocks with parameters. These enable you to produce different MOF files by varying the parameter values that are used when you execute the configuration.
For example, suppose you wanted to add a feature to a node. You could create a specific configuration where you hard code the feature name and the node name. This is not dissimilar to how you copied specific files from DC1
to SRV1
in the Using DSC and built-in resources recipe.
With parameterization, you create a configuration that takes the node name and the service name as parameters. When you run the configuration, PowerShell creates a MOF file that adds the specified service to the specified node. This recipe demonstrates that approach.
This approach throws up the problem that, by default, you can only send a single MOF file to a given node; therefore, if you used the earlier recipe and copied files to SRV2
, attempting to send a second MOF file to the system results in an error. There are three solutions to this, as described in the following list:
SRV2
that DSC created when you pushed the configuration. By removing the files from the node, DSC is happy to apply another configuration.This recipe uses SRV1
and SRV2
, as configured in the Using DSC and built-in resources recipe.
SRV2
using the following code:Get-WindowsFeature DNS -ComputerName SRV2
Configuration ProvisionServices { param ( [Parameter(Mandatory=$true)] $NodeName, [Parameter(Mandatory=$true)] $FeatureName ) Import-DscResource –ModuleName 'PSDesiredStateConfiguration' Node $NodeName { WindowsFeature $FeatureName { Name = $FeatureName Ensure = 'Present' IncludeAllSubFeature = $true } # End Windows Feature } # End Node configuration } # End of Configuration document
$NIHT = @{ Path = 'C:DSC ' ItemType = 'Directory' ErrorAction = 'SilentlyContinue' } New-Item @NIHT| Out-Null Get-ChildItem -Path C:DSC | Remove-Item -Force | Out-Null
SRV2
using the following code:$RIHT =@{ Path = '\SRV2C$WindowsSystem32configuration*.mof' ErrorAction = 'SilentlyContinue' } Get-Childitem '\SRV2C$WindowsSystem32configuration*.MOF' | Remove-Item @RIHT -Force
ProvisionServices
function to create the MOF to provision DNS on SRV2
, using the following code:$PSHT = @{ OutputPath = 'C:DSC' NodeName = 'SRV2' FeatureName = 'DNS' } ProvisionServices @PSHT
Start-DscConfiguration -Path C:DSC -Wait -Verbose
SRV2
using the following code:Get-Service -Name DNS -ComputerName SRV2
In step 1, you check to see whether the DNS server service is installed on SRV2
. The output showing that DNS is not installed looks like this:
In step 2, you create a parameterized configuration (ProvisionServices
) that takes a feature name and a node name as parameters. In step 3, you ensure that C:DSC
is created on SRV1
and is empty (that is, with no MOF files left over from earlier recipes!). In step 4, you clear out any MOF files on SRV2
. These steps produce no output.
In step 5, you create a MOF file, specifying the name of the feature (DNS
) you wish to install on the node (SRV2
). The output looks like this:
In step 6, you use Start-DscConfiguration
to push the configuration to SRV2
, which looks like this:
In the final step, step 7, you recheck the DNS Server
service on SRV2
to find out whether it is installed and running, which looks like this:
In step 2, you create a simple parameterized configuration statement. This configuration takes two parameters: a feature name and a node name. The purpose of the configuration block is to add a named feature (for example, DNS) to a named node (SRV2
). With this configuration, you can create the necessary MOF file to add any feature to any node.
In step 4, you clear any previously created MOF files from SRV2
. If you delete a previously pushed MOF file, the configuration set by those configuration MOF files does not change. This enables you to use SRV2
to test different configurations or multiple DSC recipes.
18.118.32.222