Windows PowerShell Desired State Configuration
1. Configuration Instance Document The Windows PowerShell script that
generatesthedefinitionofthedesiredstate.
2. MOF file TheManagedObjectFormat(MOF)filethatcontainsthecompiled
definitionofthedesiredstate.
3. Local Configuration Manager The DSC engine on the client system that executesagainsttheMOFtoretrievethecurrentconfiguration,comparethe
currentconfigurationtotheMOF,and/orapplythesettingsdefinedintheMOF.
4. Desired State Configuration Pull Server (optional) The repository for the definitionofthedesiredstate.IfclientsareconfiguredtoexecuteaPULL
insteadofreceivingaPUSH,theDSCPullServerhoststheconfigurationand
client data required to remediate.
To begin, you need to ensure Windows PowerShell Remoting is enabled on all involved systems (Enable-PSRemoting). I highly recommend leveraging group policy to put this in place. Group policy has been helping IT professionals enforce policy for more than a decade. It is a perfect complement to your DSC implementation.
NOTE In addition to requiring PSRemoting, DSC applies only to x64 bit system with Windows PowerShell 4.0.
Next,generatetheConfigurationInstanceDocument.ThisWindowsPowerShell
scriptcontainsthedefinitionandexecutionorderofwhatshouldexistonthetarget
machine. In this example, your front-end web server requires IIS installed, a set of files(content),anapplicationinstalled(setupfilesuchasanMSI)aswellasspecific
registrykeys.TheConfigurationInstanceDocumentisascriptthatcontainsa
configurationblockofcode.Thatconfigurationblockcontains:
■
■ Node(s) Aspecifictargetsystem;definedas“localhost”;oranytargetednode
system(definedinahashtable).
■
■ Resource(s) Theactualconfigurationitemsthatyouarelookingforaswell
as their dependencies. For example, the application that must be present has aprerequisiteofIIS.Tworesourceswouldbespecified:aresourcenamethat
contains Web-Server and the resource name that contains the application MSI. The applicationresourcewouldhaveadependencysetfortheresourcethatdefines
Web-Server (see http://technet.microsoft.com/en-us/library/dn249921.aspx).
DSC is extremely extensible and modular. Your script can be very simple or be scaled out. In Windows PowerShell, you create functions and modules for tasks that you call frequently or leverage from multiple scripts. In DSC you can separate configurationdataandyourlogicaswelltomakeitmoremodular.
YoucancontroltheactionsoftheLocalConfigurationManager.Windows
PowerShellDSCcmdletsGet-DscLocalConfigurationManagerand
Set-DscLocalConfigurationManagerareusedtocalltheLocalConfiguration
Managerengine.UsingtheWindowsPowerShellDesiredStateConfigurationSettings
keywordinascript,youcangenerateaseparateMOFfilethatcontainsan
instructionsetfortheLocalConfigurationManagerengine.TheMOF-generating
scriptcanchangethefunctionalityoftheenginefromreceivingconfiguration
information via a PUSH to executing a PULL from a DSC server. You can change frequencysothatitrefreshesitsconfigurationdata:
Once the script is built, review it to ensure you have the external dependencies set up:
Configuration CommerceConfig {
# A Configuration block can have zero or more Node blocks Node "FE-WEB-01"
{
# Next, specify one or more resource blocks
# WindowsFeature is one of the built-in resources you can use in a Node block
# This example ensures the Web Server (IIS) role is installed WindowsFeature CommerceSite
{
Ensure = "Present" # To uninstall the role, set Ensure to
"Absent"
Name = "Web-Server"
}
# File is a built-in resource you can use to manage files and directories
# This example ensures files from the source directory are present in the destination directory
File CommerceSite {
Ensure = "Present" # You can also set Ensure to "Absent"
Type = "Directory" # Default is "File"
Recurse = $true
SourcePath = $WebsiteFilePath # This is a path that has web files
DestinationPath = "C:\inetpub\wwwroot" # The path where we want to ensure the web files are present
Requires = "[WindowsFeature]CommerceSite" # This ensures that MyRoleExample completes successfully before this block runs
} } }
$WebsiteFilePath = "\\2012R2PREVIEW\CommerceSiteSource\SiteData"
CommerceConfig
Doesyourwebserverrequirefilecontent?Ifso,didyousetupasharefor
that content to be pulled from? Your DSC Pull Server might make a good repository for these items. In the example above, the share is \\2012R2PREVIEW\
CommerceSiteSource;however,thedataislocatedin\SiteData.Thepathspecified
forfilescannotbetherootofashare,butmustbeafolderunderneaththatshare.
ExecutetheConfigurationInstanceDocument(yourWindowsPowerShellscript)to
generatetheMOFfilecontainingtheconfigurationinstructions:
Now comes the really cool part—deploying the DSC job to the client system. The –PathswitchpointstothedirectorywheretheMOFfileexists(onthesystemwhere
the script is being executed from):
If you include the –Wait and –Verbose switches, you can see the progress of the job (and any errors if they occur):
ChecktheOperationalandDebugeventlogsforDSC.Youcanfindtheselogsin
Event Viewer under Applications And Services Logs | Microsoft | Windows | Desired StateConfigurationorApplicationsAndServicesLogs| Microsoft | Windows | DSC.
Youcanthentesttheappliedconfiguration.Thetestcomparesthedesiredstate
configurationtothetargetsystemandreturnsa“True”or“False”result:
Asaverificationtest,deleteafewfilesfromthewebservercontentdirectory
C:\inetpub\wwwroot and perform the test. Note that the returned result is “False.”
ExecutethesameDscConfigurationjobrunpreviously.Completethetestandnow
you can see the returned result is again “True” because the issues were remediated:
YourenvironmentandrequirementsdefinehowfaryouextendDSC.Forexample,
if you have Microsoft System Center Orchestrator or an existing automation tool within your environment, you may choose to leverage that to manage checking state and performing remediation if necessary.
If that is not an available option, you may choose to switch the client from receiving aPUSHtonowautoremediatingviaaPULLtoextractitsMOFfilefromaDesired
StateConfigurationPullServer.ThisisawebserviceutilizingtheODATAIIS
extension. The DSC Pull Server can be set up on Windows Server 2008 R2, 2012, or 2012 R2. For Windows Server 2008 R2 and 2012, you can install the role through a set of Windows PowerShell commands that essentially install all of the necessary components. In Windows Server 2012 R2, the DSC Pull Server is a feature:
Advancements in Windows PowerShell continue to transform the data center landscape. Features like DSC demonstrate how the push toward IaaS is not only achievable, but is here to stay.
Jeff Butte
Senior Consultant, US Public Sector