Custom

If neither built-in nor community resources fit your requirements, you have to use custom-built resources—that is, you have to write the code yourself. These resources can either be class-based (PowerShell 5+) or MOF-based (PowerShell 4+).

DSC resources always have the same three functionalities, regardless of their type:

  • Get/Get-TargetResource: A method or function that returns the current state of the system. It is often used internally in the test method later on. Get-DscConfiguration will display the current system status with the information retrieved from this method/function. This method always returns an instance of the class or, if MOF-based, a hashtable with all of the parameter names as keys.
  • Test/Test-TargetResource: A method or function that tests whether the system is currently in the desired state. Returns $true if it is and $false otherwise.
  • Set/Set-TargetResource: A method or function that carries out the configuration of the resource if the test method returned $false and the system state needs to be corrected:

If you are writing MOF-based resources, you might want to use the PowerShell xDSCResourceDesigner module, which will make your life a little easier. Class-based resources are already as easy as they can be, with the only slight issue being that you cannot use them with systems running PowerShell 4 and that you currently cannot test them properly with Pester. All resources can use additional helper functions, which you can define in one or more modules or plain script files:

# Roll you own resource module with resources
$resourceProperties = @(
# Each resource needs one key - the indexing property
New-xDscResourceProperty -Name Path -Type String -Attribute Key -Description 'The path to clean up'
New-xDscResourceProperty -Name FileAge -Type DateTime -Attribute Required -Description 'The maximum file age (last modified)'
)

# Creates your resource module folder (xFileResources)
# with a subfolder DSCResources containing your resource, xFileCleaner
New-xDscResource -Name xFileCleaner -Property $resourceProperties -Path .xFileResources

The xDscResourceDesigner module will preseed your files so that you can start developing immediately. This includes the resource module, as well as the MOF file containing the CIM class definition. Alternatively, simply use the VS Code/ISE-snippet DSC resource provider (with class) or DSC resource with class (simple) for a class-based resource:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset