Invoke-DscResource removes the need for a configuration file but also removes a lot of the LCM's functionality. This command moves decisions from the LCM to the user.

Prior to Windows Management Framework (WMF) v5, people that wanted to use Desired State Configuration (DSC) to apply configurations to their machines were forced to go through the same procedure if they wanted to provision an entire server or just install a single Windows feature:

  • Create the DSC configuration script
  • Compile the script into a MOF file
  • Deliver the MOF file somehow to the machine
  • Invoke the Local Configuration Manager (LCM) to consume that MOF file

This process works, but there are times when you'd rather not go through all the hassle of building a configuration in the first place. Wouldn't it be nice if you could apply a minor change via DSC without having to create a configuration and deal with a MOF file? That's what the PowerShell Team gave us in WMF 5.0 in the form of the command Invoke-DscResource.

In this article, we're going to look at building a configuration to install a single Windows feature on the local machine and doing so via Invoke-DscResource, to see the difference between these two approaches.

Let's first enable the telnet client Windows feature via the "old-fashioned" way of creating a configuration, delivering the MOF, and telling the LCM to consume the MOF.  To do so, we'll start out with a simple DSC configuration.

Configuration InstallWindowsFeature {
    Node 'localhost' {
        WindowsFeature 'TelnetClient' {
            Ensure = 'Present'
            Name = 'Telnet-Client'

Once we've got the configuration built, we'll then convert this into a MOF file that the LCM can understand by executing the script. This gives us a localhost.mof file.

DSC Configuration

DSC Configuration

Once we've got the MOF file, I can now run Start-DscConfigurationManager to tell the LCM it has a new configuration to process.

Running a DSC Configuration

Running a DSC Configuration

The Telnet-Client Windows feature is now under the control of DSC. Let's now see how this is done by just using the new Invoke-DscResource.

Invoke-DscResource -Name WindowsFeature -Method Set -Property @{Name = 'Telnet-Client'} -Verbose -ModuleName PSDesiredStateConfiguration

Done. Well, not technically, but you get the point here. Invoke-DscResource can forego the configuration and MOF manipulation altogether and allow you to invoke the Get, Set, and Test methods manually. It gives you granular control over the DSC resources.

Notice, though, that by using this method, you're not getting one of the biggest benefits of DSC which is the "set it and forget it" mentality. At this point, you are taking on the responsibility that the LCM previously had by running the Test method first to see what kind of state the machine is in and then, and only then, running the Set method if the machine is not in the desired state.

Technically, using Invoke-DscResource as below would be the same workflow that the LCM goes through when applied via a configuration.

$commonParams = @{
    Name = 'WindowsFeature'
    Property = @{ Name = 'Telnet-Client' }
    ModuleName = 'PSDesiredStateConfiguration'
    Verbose = $true
$state = Invoke-DscResource @commonParams -Method Test
if (-not $state.InDesiredState) {
    Invoke-DscResource @commonParams -Method Set

When this is run, you'll see that the Test method is run first, and only if the LCM detects that the machine isn't in the correct state will the Set method then run.

Subscribe to 4sysops newsletter!

Note: When using Invoke-DscResource extensively Microsoft recommends disabling the LCM. After all, you are replacing what the LCM does when calling methods directly, so this makes sense. Although Invoke-DscResource will work while the LCM is enabled, you'll find that your scripts referencing Invoke-DscResource will begin to fight with the automatic nature of the LCM.


Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2021


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account