We will now explain how to compile and apply DSC Configurations.
Getting started
Software dependencies
Please make sure to have the followings PowerShell Modules installed before proceding:
- Baselinemanagement
- WindowsDefender
- PowerShellAccessControl
If necessary install them:
-
Download ‘PowerShellAccessControl’ by following this link: https://gallery.technet.microsoft.com/scriptcenter/PowerShellAccessControl-d3be7b83 and copy the downloaded folder in the following path: ‘C:\Program Files\WindowsPowerShell\Modules’
-
To install the other modules simply type the commands in the PowerShell
Install-Module Baselinemanagement Install-Module WindowsDefender
possible notifications in PowerShell:
NuGet provider is required to continue PowerShellGet requires NuGet provider version ‘2.8.5.201’ or newer to interact with NuGet-based repositories. The NuGet provider must be available in ‘C:\Program Files\PackageManagement\ProviderAssemblies’ or ‘C:\Users$env:UserName\AppData\Local\PackageManagement\ProviderAssemblies’. You can also install the NuGet provider by running ‘Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force’. Do you want PowerShellGet to install and import the NuGet provider now? [Y] Yes [N] No [S] Suspend [?] Help (default is “Y”):
Press “Y” and move on.
Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from ‘PSGallery’? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is “N”):
Press “A” and move on.
The XOSS-Module contains custom XOSS-Resources we want to use later on in our own dsc configuration script, so copy the XOSS-Module to C:\Program files\WindowsPowerShell\Modules. Open an elevated PowerShell and change to this root directory.
Ensure DSC Resources are present with:
Get-DscResource -Name XOSS* -Module XOSS*
Compile DSC Configuration
Once you have successfully ensured that you got all neccessary dependencies you can safely generate a dsc configuration document for W2k16 by invoking either our default W2k16_1607.ps1 script or a self-written configuration script, because before you can enact a configuration, you have to compile it into a MOF document.
A dsc configuration script resembles this structure:
configuration "*HowYouWantToNameYourConfig*" {
Import-DSCResource -ModuleName "*ModuleName*"
Node "*ComputerName*"
{
...
*UsedDSCResource1* "HowYouWantToNameYourDSCResource1"{...}
*UsedDSCResource2* "HowYouWantToNameYourDSCResource2"{...}
*UsedDSCResource3* "HowYouWantToNameYourDSCResource3"{...}
...
}
}
*HowYouNamedYourConfig*
Our default configuration script
To call a configuration script, the function must be in global scope (as with any other PowerShell function). You can make this happen either by “dot-sourcing” the script, or by running the configuration script by using F5 or clicking on the Run Script button in the ISE. To dot-source the script, run the command . .\myConfig.ps1 where myConfig.ps1 is the name of the script file that contains your configuration.
In our scenario that would be simply:
.\W2k16_1607.ps1
Write a configuration yourself
As we remember, a DSC configuration script resembles this structure:
configuration "*HowYouWantToNameYourConfig*" { Import-DSCResource -ModuleName "ModuleName" Node localhost { ... *DSCResource1* "HowYouWantToNameYourDSCResource1"{} *DSCResource2* "HowYouWantToNameYourDSCResource2"{} *DSCResource3* "HowYouWantToNameYourDSCResource3"{} ... } } *HowYouNamedYourConfig*
but now you got to write it yourself, so try it out and use eligable dsc resources.
To check eligable dsc resources write
Get-DscResource
and read into the resources on https://docs.microsoft.com/en-us/powershell/scripting/dsc/resources/resources?view=powershell-7
After writing your own configuration script, invoke it and generate a dsc configuration document.
At the same folderpath of our used script there will be a new directory called “NameOfConfig” afterwards and powershell will prompt:
Directory: *path of default script*\*NameOfConfig*
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/29/2020 7:35 PM 564704 localhost.mof
The MOF file contains all of the configuration information for the target node. Configuration documents (MOF files) can be applied to the machine using the Start-DscConfiguration cmdlet.
Apply the configuration
Now that you have the compiled MOF, you can apply the configuration to the target node (in this case, the local computer) by calling the Start-DscConfiguration cmdlet.
Start-DscConfiguration -Path "*path of default script*\W2K16_1607" -Force -Wait -Computername localhost
If the cmdlet runs without any errors everything went fine and according to plan.