DevOps: PowerShell Desired State Configuration
Author: jason
Date: 2015-09-01 10:55:57
Category: Technical

The PowerShell Desired State Configuration (DSC) feature is a pretty cool way to summarize the state you want a server to be in. Execute the script, and the Local Configuration Manager (LCM) handles getting the server to that state.

PS/DSC is available with PowerShell v4.0. Windows 2008 R2 servers need to have the Windows Management Framework 4.0 installed. Windows 2012 R2 server come with it by default.

For instance, with DSC we can specify the Web Server feature must be present, instead of specifying how and where to get/install the Web Server.

Code

WindowsFeature IIS {
Name = "Web-Server"
Ensure = "Present"
}


There are 2 parts of the PS/DSC. The Configuration and the Config Data (Environment). The Configuration is how we want the server to be. The Config Data is the server(s) we want to do it to.

Code

# This is where we specify the servers we want to execute against
$ConfigurationData =
@{
AllNodes =
@(
@{
NodeName = "localhost" # or some servername
}
);
} # end config data

# This sets up the "state" we want those above servers to be in
Configuration QuickTest {

# Start configuration actions
Node $AllNodes.NodeName {

File MakeTemp
{
Ensure = "Present"
Type = "Directory"
DestinationPath = "c:temp"
} # end state

} # end node

} # end config

# This makes the MOF file(s)
QuickTest -ConfigurationData $ConfigurationData –Verbose

# This runs the MOF file
Start-DSCConfiguration -Wait -Force -Verbose -Path .QuickTest


References
DSC Overvier: https://technet.microsoft.com/en-us/%5Clibrary/Dn249912.aspx
Get Started with PS/DSC: https://technet.microsoft.com/en-us/%5Clibrary/Dn249918.aspx
Info about DSC: http://blogs.technet.com/b/privatecloud/archive/2014/04/25/desired-state-configuration-blog-series-part-1-learning-about-dsc.aspx
More DSC: http://blogs.msdn.com/b/powershell/archive/2013/11/01/configuration-in-a-devops-world-windows-powershell-desired-state-configuration.aspx
Tips: https://technet.microsoft.com/en-us/magazine/ff700227.aspx
Enable-PSRemoting: https://technet.microsoft.com/en-us/library/Hh849694.aspx
Windows Management Framework 4.0 Download: http://www.microsoft.com/en-us/download/details.aspx?id=40855
Windows Management Framework 5.0 Preview Download: https://www.microsoft.com/en-us/download/details.aspx?id=46889
WinMF 5.0 Preview: http://blogs.msdn.com/b/powershell/archive/2015/04/29/windows-management-framework-5-0-preview-april-2015-is-now-available.aspx
More DSC: http://blogs.citrix.com/2014/09/25/desired-state-configuration-how-does-it-work/




jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009