DSC Syntax

Here is a classic DSC configuration structure:

Configuration ExampleConfig
{  
  
  param(
    [Parameter(Mandatory=$true)] 
    [String[]]$Param1, 
    [Parameter(Mandatory=$true)] 
    [String]$Param2, 
    [Parameter(Mandatory=$true)] 
    [String]$Param3
  ) 

  Node $Servers 
  {  
    File ResourceName 
    { 
        ...  
    } 
  }  
} 

The word “Configuration” is followed by the name of the configuration. You are free to write what you wish. The node name(s) you enter here will be display in the name column in the config.XO frontend.

The parameters block contains the parameters that are used in this configuration

The node block tells DSC to deploy this configuration on these machines

This is the resource you want to configure. The File resource is followed by a unique friendly name for this resource.

Example

This simple example copies the “hosts” file on two nodes. It contains three mandatory parameters ($Servers, $SourceFile, $DestinationFile):

Configuration CopyHostsFile 
 
{  
  
  param( 
    [Parameter(Mandatory=$true)] 
    [String[]]$Servers, 
    [Parameter(Mandatory=$true)] 
    [String]$SourceFile, 
    [Parameter(Mandatory=$true)] 
    [String]$DestinationFile
  ) 

  Node $Servers
  {  
    File HostFile 
    { 
        Ensure = "Present" 
        Type = "File" 
        SourcePath = $SourceFile
        DestinationPath = $DestinationFile
    } 
  } 
} 

Get DSC Resource Syntax

How do we start with the DSC resource if we need to find out the correct syntax? This is pretty easy with the following command:

PS > Get-DSCResource File -syntax

File [String] #ResourceName
{
    DestinationPath = [string]
    [Attributes = [string[]]{ Archive | Hidden | ReadOnly | System }]
    [Checksum = [string]{ CreatedDate | ModifiedDate | SHA-1 | SHA-256 | SHA-512 }]
    [Contents = [string]]
    [Credential = [PSCredential]]
    [DependsOn = [string[]]]
    [Ensure = [string]{ Absent | Present }]
    [Force = [bool]]
    [MatchSource = [bool]]
    [PsDscRunAsCredential = [PSCredential]]
    [Recurse = [bool]]
    [SourcePath = [string]]
    [Type = [string]{ Directory | File }]
}

This process is working with every available DSC resource available.

A good starting point for the DSC resources is on Github.