Let’s imagine that you’re working on a fairly complex DSC configuration and you made a small change to the section shown below which you’d like to test.
| 001 002 003 004 005 006 007 | File 'ConfigFile' { Type = 'File' Ensure = 'Present' SourcePath = '\\dc1\ConfigFiles\config.txt' DestinationPath = 'C:\configs\config.txt' } |
For the sake of this imaginary scenario, let’s assume you’re using a pull server. One way you could test your change is to do the following:
- Generate the mof file
- Copy it to the pull server
- Create the checksum
- Run the DSC configuration on your target node to test
While this test will certainly work, you’re now reapplying the whole configuration just to test that one small section which you changed. If you got something wrong and it fails, you have to repeat the whole process again to test it a second time. In many cases, I have found this to be overkill and a major time waster when all I’m trying to do is verify that my change works as expected. For scenarios like this there’s a much easier way thanks to the little-known Invoke-DscResource cmdlet. Let’s take a look at how it works.
The code below shows the same configuration as above but instead of being part of a larger configuration that has to be applied via your pull server, it’s now a standalone piece of code that you can test anywhere.
| 001 002 003 004 005 006 | Invoke-DscResource -ModuleName PsDesiredStateConfiguration -Name File -Method Set -Property @{ Type = 'File'; Ensure = 'Present' SourcePath = '\\dc1\ConfigFiles\config.txt'; DestinationPath = 'C:\configs\config.txt'; } -Verbose |
Notice that we’ve taken our configuration from above and simply placed it in a hash table which we pass to the Property parameter. The screenshot below shows the output after running the command – exactly the same as if we’d gone through the lengthy process of creating the mof and pulling it from our pull server!
Another powerful use for Invoke-DscResource is that you can use it to verify whether a server is in the desired state for a given configuration. All you need to do is change the Method parameter from Set to Test.