Create Desired State Configuration (DSC) “snippets”-like resources

In the following help file…

Get-help about_DesiredStateConfiguration

… there’s a cool tip to show the syntax of the DSC resources keywords

DSC provides the following set of built-in resources that you can use in a configuration script: Registry, Script, Archive, File, WindowsFeature, Package, Environment, Group, User, Log, Service, and WindowsProcess. The example above demonstrates how to use the File and WindowsFeature resources. To see all the properties that you can use with a given resource, place the cursor on the resource keyword (for example, File) within your configuration script in the Windows PowerShell ISE, hold down CTRL, and press the SPACEBAR.

If I press CTRL+spacebar, I’ve the following:

That’s cool, but it doesn’t tell me which one is mandatory… 😦

Here’s what I did to generate “snippet”-like DSC resources:

[System.Management.Automation.Language.DynamicKeyword]::GetKeyword() | ? { $_.Keyword -in @(
) } | ForEach-Object -Begin {
    'Configuration TestDSCConfig {'
} -Process { 
    "`t{0} Test{0} " -f ($_.Keyword)
    $_.Properties.Values | ForEach-Object -Process { 
        if ($_.Mandatory) {
            "`t`t{0} = {2} # {1} ; Manadatory" -f $_.Name,$_.TypeConstraint,$($_.Values -join "|")
        } else {
            "`t`t{0} = {2} # {1}" -f $_.Name,$_.TypeConstraint,$($_.Values -join "|")
    } -End {
} -End {

Now, all I need to do is to remove properties I don’t need, fill-in mandatory properties… 😀


One thought on “Create Desired State Configuration (DSC) “snippets”-like resources

  1. In Windows 8.1 / 2012 R2 RTM, the function Get-DynamicKeyword of the PSDesiredStateConfiguration module has been renamed to Get-DSCResource.
    Powershell MVP Ravikanth Chaganti shows on Powershell Magazine in the following tip some cool features on how to use this new Get-DSCresource cmdlet.
    Internally, this function still uses the .Net System.Management.Automation.Language.DynamicKeyword class.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s