Parameterizing DSC configuration

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:

  • Have a single MOF file generated for each target node. This means larger MOF files, and files that are used for larger organizations sometimes require hard-to-achieve coordination between the different groups that create the overall configuration for a node.
  • Use DSC partial configurations, which enables you to send multiple configurations to a node. You configure the node to pull different configuration blocks from potentially multiple DSC pull servers, and then the DSC's local configuration manager combines and then applies them. The Using DSC partial configuration recipe shows you how to use partial configurations.
  • Push a configuration to a node, remove the node's MOF files, and send another configuration. This recipe demonstrates this—you remove the MOF files on SRV2 that DSC created when you pushed the configuration. By removing the files from the node, DSC is happy to apply another configuration.

Getting ready

This recipe uses SRV1 and SRV2, as configured in the Using DSC and built-in resources recipe.

How to do it…

  1. Check the status of DNS on SRV2 using the following code:
    Get-WindowsFeature DNS -ComputerName SRV2
  2. Create a configuration document using the following code:
    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
  3. Ensure that an empty DSC folder exists, then create MOF file using the following code:
    $NIHT = @{
      Path        = 'C:DSC '
      ItemType    = 'Directory'
      ErrorAction = 'SilentlyContinue'
    }    
    New-Item  @NIHT| Out-Null
    Get-ChildItem -Path C:DSC | Remove-Item -Force | Out-Null
  4. Clear any existing configuration documents on SRV2 using the following code:
    $RIHT =@{
      Path        = '\SRV2C$WindowsSystem32configuration*.mof'
      ErrorAction = 'SilentlyContinue'
    }
    Get-Childitem '\SRV2C$WindowsSystem32configuration*.MOF' |
      Remove-Item @RIHT -Force
  5. Now run the 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
  6. And make it so:
    Start-DscConfiguration -Path C:DSC -Wait -Verbose
  7. Check the results on SRV2 using the following code:
    Get-Service -Name DNS -ComputerName SRV2

How it works...

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:

How it works...

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:

How it works...

In step 6, you use Start-DscConfiguration to push the configuration to SRV2, which looks like this:

How it works...

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:

How it works...

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.32.222