Export (0) Print
Expand All

Windows PowerShell Desired State Configuration Data

Published: June 24, 2013

Updated: June 24, 2013

Applies To: Windows PowerShell 4.0

In Windows PowerShell Desired State Configuration (DSC), it is possible to separate configuration data from the logic of your configuration. This has the following advantages:

  • It allows you to reuse your configuration data for different resources, nodes, and configurations.

  • Configuration logic is more reusable if it does not contain hard-coded data. This is similar to good scripting guidelines for functions.

  • If some of the data needs to change, you can make the changes in one location, thereby saving time and reducing errors.

Rules and options for separating data

Below is a demonstration of how you can separate out configuration data.

  1. Define a hash table. In the example below, we named it $ConfigData, but you can give it any name you choose.

  2. Inside the hashtable, define at least one array, which you must name $AllNodes. You are free to define additional arrays inside the hash table, if you need them for other uses in your script.

  3. Inside the $AllNodes array, define zero or more hash tables that will contain configuration data.

  4. Each of the hash tables that are direct elements of $AllNodes must have at least one property called NodeName, and the values must be represented as a string.

  5. When you invoke your configuration, pass the $AllNodes array to it by using the automatic ConfigurationData parameter that is built-in and available for use with all configurations.

  6. Within the configuration, access the configuration data array by using the $AllNodes automatic variable.

  7. You can use the keyword Where to search the properties of the hash tables that form the $AllNodes array.

  8. After you filter with Where, you can access the selected set of hash tables by using the $SelectedNodes automatic variable.

  9. The $AllNodes array can have a hash table where the NodeName property is set to "*", so that any properties you define in this hash table will be available to all nodes. When a node-specific hash table (that is, a hash table with NodeName set to a string other than "*") defines a property with the same name as a property defined in the "*" hash table, the value of the property as defined in the node-specific hash table overrides the value defined in the "*" hash table.

$ConfigData = @{
    AllNodes = @( 
             # NodeName "*" applies globally to all nodes in this array
             @{ NodeName = "*"; RecurseValue = $true },
             @{ NodeName = "localhost"; Role = "Web"; RolesToBePresent = "Web-Server"; 
                SourceRoot = "\\Server106\source\presentation\"; Version = "1.0"; 
                WebDirectory = "c:\inetpub\wwwroot\"; RecurseValue = $false; }
    );

    # Environmental configuration for the DHCP settings
    # These settings can be generic configuration data that is not node specific. 
    DHCPConfigEnv = @{
                    ComputerName = "((Get-VM dc | Select-Object -ExpandProperty networkadapters).ipaddresses)[0]"
                    DNSIPAddress = $dcIPAddress
                    DNSName = "dc.fourthcoffee.com"
                    ScopeName = "PowerShellSummit2013"
                    ScopeID = "192.168.10.0"
                    StartRange = "192.168.10.3"
                    EndRange = "192.168.10.254"
                    SubnetMask = "255.255.255.0"
                    LeaseDurationDays = 1
                    DomainName = $DomainName
    };
}

Configuration CloudService
{   # The $AllNodes and $Node (current node) variables are automatic variables
    Node $AllNodes.Where{$_.Role -eq "Web"}.NodeName {
        WindowsFeature IIS
        { Ensure = "Present"; Name = $Node.RolesToBePresent }
    }
}
 
CloudService –ConfigurationData $ConfigData 



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft