Holiday Gift – Desired State Configuration (DSC) Resource Kit Wave-1

Holiday Gift – Desired State Configuration (DSC) Resource Kit Wave-1

Rate This
  • Comments 18

Continuing with the tradition of holiday gifts to the PowerShell community, the PowerShell team has just released DSC Resource Kit Wave-1 - a set of PowerShell modules that contain DSC resources and example configurations. The various modules that are part of DSC Resource Kit Wave 1 can be found here.


When DSC was introduced in PowerShell v4, we shipped a set of built-in resources. However one of the important features of DSC is the ability to create custom resources in PowerShell. Our previous blog posts detail how to author resources and how to deploy resources. In order to encourage the community to create more DSC resources and help boot strap the authoring process, we are releasing this first wave.


We have introduced a new naming convention for these modules and resources – they contain an “x” in them like xWebAdministration, MSFT_xWebsite, etc. The “x” stands for experimental – which means these resources are provided AS IS and are not supported through any Microsoft support program or service. We will monitor these, take feedback and may provide fixes on a “fix forward” basis – that is to say we may simply republish with fixes in future. I am deliberately using the word “may” to indicate no guarantees of any sort. However, you are free to adapt these to your environment and make changes as necessary.


Description of Resources


To discover all the resources available as part of the resource kit, use the Get-DSCResource cmdlet:


PS D:\> Get-DscResource -Name x* | Format-Table Name, Module, ImplementedAs -AutoSize

 

Name              Module                       ImplementedAs

----              ------                       -------------

xComputer         xComputerManagement             PowerShell

xVHD              xHyper-V                        PowerShell

xVMHyperV         xHyper-V                        PowerShell

xVMSwitch         xHyper-V                        PowerShell

xDNSServerAddress xNetworking                     PowerShell

xIPAddress        xNetworking                     PowerShell

xDSCWebService    xPSDesiredStateConfiguration    PowerShell

xWebsite          xWebAdministration              PowerShell

 

Here is a brief description about each of the resource


Resource

Description

xComputer

Name a computer and add it to a domain/workgroup

xVHD

Create and managed VHDs

xVMHyperV

Create and manage a Hyper-V Virtual Machine

xVMSwitch

Create and manage a Hyper-V Virtual Switch

xDNSServerAddress

Bind a DNS Server address to one or more NIC

xIPAddress

Configure IPAddress (v4 and v6)

xDSCWebService

Configure DSC Service (aka Pull Server)

xWebsite

Deploy and configure a website on IIS

 

 

 

Making Changes to Resources

 

When making changes to these resources, we suggest the following practice:

1.     Update the following names by replacing MSFT with your company/community name and replacing the “x” with "c" (short for "Community") or another prefix of your choice:

a.     Module name (ex: xWebAdministration becomes cWebAdministration)

a.     Folder name (ex: MSFT_xWebsite becomes Contoso_cWebsite)

b.     Resource Name (ex: MSFT_xWebsite becomes Contoso_cWebsite)

c.     Resource Friendly Name (ex: xWebsite becomes cWebsite)

d.     MOF class name (ex: MSFT_xWebsite becomes Contoso_cWebsite)

e.     Filename for the <resource>.schema.mof (ex: MSFT_xWebsite.schema.mof becomes Contoso_cWebsite.schema.mof)

2.     Update module and metadata information in the module manifest

3.     Update any configuration that use these resources

 

We reserve resource and module names without prefixes ("x" or "c") for future use (e.g. "MSFT_WebAdministration" or "Website").  If the next version of Windows Server ships with a "Website" resource, we don't want to break any configurations that use any community modifications.  Please keep a prefix such as "c" on all community modifications.

 

As specified in the license, you may copy or modify this resource as long as they are used on the Windows Platform.


Requirements


The DSC Resource Kit requires Windows 8.1 or Windows Server 2012 R2 with update KB2883200 (aka the GA Update Rollup). You can check whether it is installed by running the following command:

 

PS C:\WINDOWS\system32> Get-HotFix -Id KB2883200

 

Source        Description      HotFixID      InstalledBy          InstalledOn             

------        -----------      --------      -----------          -----------             

NANA-TOUCH    Update           KB2883200     NANA-TOUCH\Admini... 9/30/2013 12:00:00 AM   

 

On supported down level operating systems, they require WMF 4.0. Refer to these previous blog posts for more information on WMF 4.0 and issues with partial installation.

 

Configurations using Resources from DSC Resource Kit


Once the resources are deployed, they can be used in configurations. An example configuration is given below (this example together with the sample website files are available as part of the examples of xWebAdministration module):

Configuration FourthCoffeeWebsite

{

    param

    (

        # Target nodes to apply the configuration

        [Parameter(Mandatory)]

        [ValidateNotNullOrEmpty()]

        [String[]]$NodeName,

 

        # Name of the website to create

        [Parameter(Mandatory)]

        [ValidateNotNullOrEmpty()]

        [String]$WebSiteName,

 

        # Source Path for Website content

        [Parameter(Mandatory)]

        [ValidateNotNullOrEmpty()]

        [String]$SourcePath,

 

        # Destination path for Website content

        [Parameter(Mandatory)]

        [ValidateNotNullOrEmpty()]

        [String]$DestinationPath

    )

 

    # Import custom resources from the module that defines it

    Import-DscResource -Module xWebAdministration

 

    Node $NodeName

    {

        # Install the IIS role

        WindowsFeature IIS

        {

            Ensure          = "Present"

            Name            = "Web-Server"

        }

 

        # Install the ASP .NET 4.5 role

        WindowsFeature AspNet45

        {

            Ensure          = "Present"

            Name            = "Web-Asp-Net45"

        }

 

        # Stop the default website

        xWebsite DefaultSite

        {

            Ensure          = "Present"

            Name            = "Default Web Site"

            State           = "Stopped"

            PhysicalPath    = "C:\inetpub\wwwroot"

            DependsOn       = "[WindowsFeature]IIS"

        }

 

        # Copy the website content

        File WebContent

        {

            Ensure          = "Present"

            SourcePath      = $SourcePath

            DestinationPath = $DestinationPath

            Recurse         = $true

            Type            = "Directory"

            DependsOn       = "[WindowsFeature]AspNet45"

        }      

 

        # Create the new Website

        xWebsite BakeryWebSite

        {

            Ensure          = "Present"

            Name            = $WebSiteName

            State           = "Started"

            PhysicalPath    = $DestinationPath

            DependsOn       = "[File]WebContent"

        }

    }

}

 

# Create the MOF file using configuration parameters

FourthCoffeeWebSite -NodeName "TestVM" `

                    -WebSiteName "FourthCoffee" -SourcePath "C:\BakeryWebsite\" -DestinationPath "C:\inetpub\FourthCoffee"

 

# Make it happen - Copy the MOF files to appropriate nodes and invoke the configuration

Start-DscConfiguration -Path  "$PSScriptRoot\FourthCoffeeWebsite" -Wait -Verbose -Force

 

# Delete the MOF files

del -Path "$PSScriptRoot\FourthCoffeeWebsite" -Recurse -Verbose

 

Note: Any resource that is not shipped as part of Windows, needs to be available in a module in PSModulePath and must be imported (using Import-DSCResource keyword) before it can be used in a configuration.

Feel free to leave your feedback in the comments section as well as use the Q&A section in the TechNet pages. You can also provide feedback here in the connect page

 

Happy Holidays and a Happy new Year !!!

 

Updated on 1/2/2014: Updated the renaming guidelines with examples

 

Narayanan (Nana) Lakshmanan

Development Lead - PowerShell DSC

Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post
  • Can anyone help me with this question:

    stackoverflow.com/.../1712065

  • Helpful!!!

  • Hi , I worked through the examples made changes to the resource files etc and generated the .mof files ok but I get this error message whenever I use the start-dscconfiguration command

    The PowerShell provider Contoso_WebAdministration does not exist at the PowerShell module path nor is it registered as

    a WMI provider.

       + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException

       + FullyQualifiedErrorId : ModuleNameNotFound

  • There are 8 resources on your list, but only 5 in the gallery. I think you forgot to upload some of them.

  • we did some additional testing if  for example we put the MSFT_xDNSServerAddress under C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources

    it works fine.

    However if we use the recommended path of $env:ProgramFiles\WindowsPowerShell\Modules  , the get-dscresource works fine

    PowerShell      xDNSServerAddress  xNetworking             {Address, InterfaceAlias, AddressFamily, De...

    PowerShell      xIPAddress                xNetworking            {InterfaceAlias, IPAddress, AddressFamily, ...

    the .mof file is created ok using the configuration keyword etc.

    However when we run start-dscconfiguration it fails , as it can't locate the .psm1 file for the test/get/set functions , any help much appreciated.

  • @ M.T.Nielsen - With the GA update (KB2883200), one module can contain multiple resources. The xHyperV and xNetworking modules contains multiple resources.

  • @gt - Did you updated the resources on the target machine as well? You might have to stop the WMI process on target machine that is hosting DSC engine for updates to be reloaded. You can find it using the following expression

    gps wmi* | ? {$_.modules.ModuleName -like "*DSC*"}

  • @powershell team ,  thanks for the help much appreciated , we tried running the get-process command gps wmi* | ? {$_.modules.ModuleName -like "*DSC*"} on the localhost/target machine where our custom DSC resources reside but it returns nothing.

    Quick question unzipping one of the examples into  <$env:psmodulepath>  C:\Program Files\WindowsPowerShell\Modules\xComputerManagement

    get-dscresource shows for this resource

    Name             Module

    xComputer     xComputerManagement

    we generate a localhost.mof file ok and it contains  ModuleName = "xComputerManagement";

    we run the command start-dscconfiguration -wait -verbose -path c:\dsconfiguration

    The PowerShell provider xComputerManagement does not exist at the PowerShell module path nor is it registered as a WMI

    provider.

    what do we need to do for it to locate the module as there is no xComputerManagement.psm1 file in the parent directory , how do we tell the DSC engine to look in .\DSResources\MSFT_xComputer\MSFT_xComputer.psm1 where the logic is for get/set/test

  • @gt - You need to have a xComputerManagement.psd1 file under xComputerManagement folder for it to be valid module. Are you missing that?

  • A Community Edition of the DSC Resource Kit is now published on PowerShell.org`s DSC Resources on GitHub:

    github.com/.../DSCResourceKit

    As advised, the resources has been renamed to “c” (short for “community”).

  • I am having the same issue as "gt".  When I put my DSC module in the recommended path of "C:\Program Files\WindowsPowerShell\Modules\MyDscModule", the

    Start-DscConfiguration -wait -Verbose -Path "MyConfiguration"

    Fails with the message :

    "The PowerShell provider MyDscModule does not exist at the PowerShell module path nor is it registered as a WMI provider.

       + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException

       + FullyQualifiedErrorId : ModuleNameNotFound

       + PSComputerName        : localhost"

    I am able to import the MyDscModule in PowerShell using "Import-Module MyDscModule"

    I verified the $env:PSModulePath variable contains the path "C:\Program Files\WindowsPowerShell\Modules" and the value is stored at the "Machine" scope ([environment]::GetEnvironmentVariable("PSModulePath","Machine"))

    If I move the module to "C:\windows\system32\WindowsPowerShell\v1.0\Modules\MyDscModule", then the Start-Configuration command works fine.  

    I did kill the wmi* process as outlined above.

    I really do not want to install our custom DSC modules into the system directory.

    Does anyone have any idea why this is behaving this way?

  • Hey Mark,

    To be clear – you should not have to install custom DSC modules into the system directory.  

    Just so I’m clear on the details of your problem, I’ll try to restate what you said above:

    • When MyDSCModule is in C:\Program Files\WindowsPowerShell\Modules\, you can import MyDSCModule using Import-Module.

    • When MyDSCModule is in C:\Program Files\WindowsPowerShell\Modules\, you can “compile” a configuration using MyDSCModule into a .mof file (e.g. running configuration “MyConfiguration” to generate the MyConfiguration folder)

    • However, when you actually try to “apply” the MyConfiguration.mof to localhost, you encounter the above error.

    This is an odd problem.  Because you can successfully compile the .mof:

    • Your problem does not appear to relate to the Update mentioned above (KB2883200).  

    • Your configuration is probably not malformed in any way (e.g. missing Import-DSCResource –module MyDSCModule)

    Because it works when MyDSCModule is in the System Directory:

    • The MyDSCModule on the target node should be the same version as the one used to generate the .mof.

    Is it possible that you have multiple versions of MyDSCModule deployed to different areas of the module path?  Is there any way you can share you configuration with us by uploading it to CodePlex or a similar site?

    Thanks,

    John Slack

    Program Manager

    PowerShell Team

  • Mark and gt,

    Regarding the above issue, I wanted to confirm that y'all were using: "Import-DscResource -Module MyDscModule" in your configuration.  Not including the explicit import can cause the issue that you were describing.

    Thanks,

    John Slack

  • I saw the same issue as Mark and gt where I was getting the error message that the PowerShell provider does not exist at the PowerShell module path nor is it registered as a WMI provider when I ran Start-DscConfiguration. In my case, I was using the xHyperV module.  I stopped all the WMI processes hosting DSC engines but that did not help.

    Interestingly, after I rebooted the system, the error disappeared.

  • While sorting out why my custom resource was not being recognized I went to apply a configuration that has Import-DscResource within it and I received the following error on a Server 2012 R2 machine when I process the configuration to create the MOF:

    PS C:\Users\Administrator> Import-DscResource

    Import-DscResource : The term 'Import-DscResource' is not recognized as the name of a cmdlet, function, script file,

    or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and

    try again.

    At line:1 char:1

    + Import-DscResource

    + ~~~~~~~~~~~~~~~~~~

       + CategoryInfo          : ObjectNotFound: (Import-DscResource:String) [], CommandNotFoundException

       + FullyQualifiedErrorId : CommandNotFoundException

    I have no idea what happened.  Unless something was uninstalled while processing an 'absent' of my application.

Page 1 of 2 (18 items) 12