Class-based DSC resource syntax

While the class-based DSC resource folder structure is much more relaxed, the syntax for a class-based DSC resource is rather strict, or slightly more so. This makes sense, as we are collapsing a lot of functionality into one file and need a reliable way of parsing the files to get the same information we got previously in several separate files.

  • Declaring the class: Instead of a PowerShell function declaration, we use a PowerShell class declaration to define our DSC resource:
powershell
[DscResource()]
class FooBar
{
<# Class code here #>
}

Instead of function, we use class, so there's not too much difference. We added a new attribute called DscResource, but if you are familiar with PowerShell V2 parameter attributes, using attributes to denote supported functionality should be easy to transition to. We use the DscResource attribute to indicate that this class is a DSC resource. This is done so that the DSC engine can locate and parse classes as DSC resources.

  • Schema: A class-based DSC resource schema defines the properties of the class that can be used by the user of the DSC resource. Instead of a separate MOF file, the properties are defined inside the class using the PowerShell attribute syntax on top of PowerShell variable declarations.
    • Declaring DSC properties is a simple as this:
powershell
[DscResource()]
class FooBar
{
[DscProperty(Key)]
[string]$Wakka
[DscProperty(Mandatory)]
[string]$Path

<# Class code here #>
}
  • There is one attribute that can be used here, DscProperty, but several values that can be passed to it to indicate what type of DSC properties are being used here:
Property | MOF equivalent | Description
---------|----------------|------------
DscProperty(Key) | Key |The property is a key for the DSC resource and required
DscProperty(Mandatory) | Required | The property is required
DscProperty(NotConfigurable) | Read | The property is read-only and is populated by the Get() function
DscProperty() | Write | This is a configurable property that is not required
  • Methods: Declaring a class and a few properties are not all that is required for a class-based DSC resource. Remember Get-TargetResource, Test-TargetResource, and Set-TargetResource in the module-based DSC resource in the previous section? In the class-based DSC resource, we have simplified this into three methods with the same prefix, all in the same class. Let's build on the previous example and add the required methods:
powershell
[DscResource()]
class FooBar
{
[DscProperty(Key)]
[string]$Wakka
<# Class code here #>
[FooBar] Get() {}
[bool] Test() {}
[void] Set() {}
}
As before with the module-based DSC resources, each method returns a specific value or none at all.
  • Get: This method returns an instance of the class instead of a hashtable, with the properties populated with the discovered values. In our preceding example, the Get method returns an instance of the FooBar class. If we were to implement the Get method, it would look like this:
powershell
[FooBar] Get()
{
$present = $this.TestForWakka($this.Path)
if ($present)
{
$this.Wakka = $this.Path
}
else
{
$this.Wakka = $null
}
return $this
}

The internal code is contrived, so forgive the silliness, but it proves the point. This tests a few things and sets the properties with values based on those tests. Note the use of the special variable $this; it's an automatic class variable that refers to the instance of the class itself. You can use it to access methods and properties defined in the class.

  • Test: This method is the equivalent of the Test-TargetResource function and returns a Boolean indicating the state of the target node. If the node is in the expected state, it returns $true. If it is not in the expected state, it returns $false.
  • Set: This method is the equivalent of the Set-TargetResource function. The Set method executes the code necessary to bring the target node to compliance. Like the module-based DSC resource, it does not return a value and indicates success by not throwing an exception instead.

Implementing the Set method will vary greatly according to your problem domain, but here is an example of a Set method that operates on the information it finds on the target node:

powershell
[void] Set()
{
$fileExists = $this.TestFilePath($this.Path)
if($this.ensure -eq [Ensure]::Present)
{
if(-not $fileExists)
{
$this.CopyFile()
}
}else{
if($fileExists)
{
Remove-Item -LiteralPath $this.Path -Force
}
}
}
..................Content has been hidden....................

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