Finding and installing DSC resources

A DSC resource is a specially crafted PowerShell module that enables DSC to configure various aspects of a node. The WindowsFeature DSC resource, for example, enables you to ensure that a particular node has a particular Windows feature installed. You can also specify that a particular Windows feature should not be present (in which case, DSC removes the feature if it were already installed).

As you saw in the Using DSC and built-in resources recipe, Microsoft does not ship very many DSC resources natively with Windows Server 2019. This is intentional—you use the PowerShell Gallery to get the DSC resources you need to do your job and not hundreds or thousands of DSC resources you never need.

The built-in DSC resources with Windows Server 2019 do not provide broad coverage, and are not sufficient for most organizations. For example, you can use the built-in File resource to copy the source files for a small web application onto a new server, as you did in the Using DSC and built-in resources recipe. But the built-in resources do not allow you to specify the application's settings (what the application's name is, which application pool it runs in, and so on). This is where add-on DSC resources from the PS Gallery come in.

In this recipe, you use the PS Gallery to find, install, and explore third-party DSC resources. In the next recipe, Using DSC with resources from PS Gallery, you use the newly installed resources.

Getting ready

In this recipe, you find, download, and install DSC resources.

How to do it…

  1. Find available repositories using the following code:
    Get-PSRepository
  2. See what DSC resources you can find using the following code:
    Find-DscResource -Repository 'PSGallery' |
      Measure-Object
  3. See what IIS-related resources might exist using the following code:
    Find-DscResource | 
      Where-Object ModuleName -match 'web|iis' | 
        Select-Object -Property ModuleName, Version -Unique |
          Sort-Object -Property ModuleName
  4. Examine the xWebAdministration module using the following code:
    Find-DscResource -ModuleName 'xWebAdministration'
  5. Install the xWebAdministration module (on SRV1) using the following code:
    Install-Module -Name 'xWebAdministration' -Force
  6. See the local module details using the following code:
    Get-Module -Name xWebAdministration -ListAvailable
  7. See what DSC resources are contained in the module using the following code:
    Get-DscResource -Module xWebAdministration
  8. Examine what is in the module using the following code:
    $Mod = Get-Module -Name xWebAdministration -ListAvailable
    $P   = $Mod.Path
    $FP  = Split-Path -Parent $P
    Get-ChildItem -Path $FP, $FPDSCResources 

How it works...

In step 1, you examine any configured repositories on your system. Depending on the recipes you have performed on SRV1, the output of this step looks like this:

How it works...

In step 2, you use the Find-DSCResource cmdlet to examine how many DSC resources are in the gallery (at the time of writing!), which looks like this:

How it works...

In step 3, you query the PS Gallery to find DSC resources that might be helpful to manage IIS. The output looks like this:

How it works...

In step 4, you look at the DSC resources in the xWebAdministration module on the PS Gallery. The output looks like this:

How it works...

In step 5, you install the module on SRV1. This step produces no output. In step 6, you examine the details of the newly installed module, which looks like this:

How it works...

In step 7, you use the Get-DscResource cmdlet to discover the specific DSC resources contained in the xWebAdministration module, which looks like this:

How it works...

In the last step, step 8, you view the files that make up the xWebAdministration module. The output looks like this:

How it works...

There's more...

This recipe describes how to obtain DSC resources from the PowerShell Gallery repository, but there are many other places that you can find DSC resources. One other repository you might consider is Chocolatey (https://www.chocolatey.org).

In step 3, you query the PS Gallery to discover DSC resources that might be useful. In this step, you find any DSC resource whose name contains either web or iis. Depending on what DSC resource you are looking for, you may need to adjust this regular expression string.

In step 8, you view the files that make up the xWebAdministration module. To learn more about how to develop DSC resources, you should examine the files in this module in more detail.

See also

In step 8, you examine the files making up the xWebAdministration module. For more information about what is inside a DSC resource module, see https://docs.microsoft.com/en-us/powershell/dsc/resources/resources.

If you cannot find the DSC resources you need, you can always create your own customized resources. There are two main methods that IT pros can use to write their own resources. You can use the traditional approach, where you both write the PowerShell code and create a MOF schema. With PowerShell 5, Microsoft introduced a much simpler method of creating a DSC resource based on PowerShell classes. For more details about the older mechanism, see https://docs.microsoft.com/en-us/powershell/dsc/resources/authoringresourcemof, and for more details about creating a DSC resource using PowerShell classes, see https://docs.microsoft.com/en-us/powershell/dsc/resources/authoringresourceclass. And finally, for the hard core folks, you can implement the resource in C# (https://docs.microsoft.com/en-us/powershell/dsc/resources/authoringresourcemofcs).

If you do decide to develop your own DSC resources, take a look at using the Resource Designer tool—a set of cmdlets that make creating DSC resources easier. See https://docs.microsoft.com/en-us/powershell/dsc/resources/authoringresourcemofdesigner for more details about this tool.

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

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