Sunday, November 17, 2013

Debugging a PowerShell DSC Custom Resource

Been working with custom DSC resources for FIM for a few weeks now and embarrassingly it just occurred to me that I can call my custom DSC resources directly.  I had been following the guidance from TechNet whereby I would call my DSC using the Start-DscConfiguration cmdlet.  It is a great way to make sure that your DSC resource is properly wired up in PowerShell but as you start to make lots of changes it gets a bit tedious.

Another option is just to load the DSC script module using the Import-Module cmdlet, then call its functions directly.  Pretty simple, and also lets you use a couple of great features such as breakpoints and splatting.

By using splatting, you can take the hashtable (almost) directly from your DSC configuration and splat it into the DSC custom resource function.  There are a couple of parameters that won’t work (such as DependsOn) but they can easily be commented out as I’ve done in the sample below.

###

### Load the module required by my DSC resource

###

ipmo FimPowerShellModule

 

###

### Load my DSC resource

###

ipmo C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\FimService_ManagementPolicyRule\FimService_ManagementPolicyRule.psm1

 

###

### The contents of this HashTable are from my DSC configuration

###

$dscSetParameters = @{

    Ensure             = "Present"

    DisplayName        = "_DscTestMpr1"

    Description        = "why is this required?"

    Enabled            = $false

    GrantPermission    = $false

    RequestType        = @('Read','Create')

    PrincipalSet       = "_DscTestSet1"

    ResourceCurrentSet = "All People"

    ResourceFinalSet   = "Administrators" 

    ResourceAttributeNames = @('DisplayName','Description')                     

    ActionWorkflowDefinition = @('_TestActionWF','_TestActionWF2')

    Credential         = $fimAdminCredential

    #DependsOn          = "[FimService_Set]_DscTestSet1"

}

 

###

### Use the HashTable to splat into my DSC function

##

Set-TargetResource @dscSetParametersVerbose

 

 

No comments: