Using PowerShell Desired State Configuration (DSC)
Share:
PowerShell Desired State Configuration (DSC) is a powerful feature of PowerShell that enables you to deploy and manage configuration data for software services, manage environment configurations, create an environment where complexity is maintained through PowerShell scripts rather than manual processes. DSC is just another step further in making your management tasks automated, and making your life as a developer, a touch easier!
Let's dive into the world of DSC with the simple analogy of a movie production. Think of DSC as a movie director. It does not do the acting but instructs and ensures all the actors which are the resources are performing their roles as per the film script.
Let's get hands-on and write our film script, metaphorically, our DSC configuration, for the pivotal scene where the protagonist in our movie "HackerCtrl" needs to ensure a specific Windows Feature is installed.
Configuration HackerCtrlScene {
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node "MovDirector1" {
WindowsFeature "Web-Server" {
Ensure = "Present"
Name = "Web-Server"
DependsOn = "[WindowsFeature]NET-Framework-45-Core"
}
}
}
HackerCtrlScene
In this script, or rather 'scene', the movie director (Node MovDirector1) ensures the actor named "Web-Server" is present to perform in the scene. If not, DSC, like a true director, will install it before proceeding. The DependsOn
keyword ensures that the actor 'Web-Server' won't show up until the 'NET-Framework-45-Core' has completed its role.
Now that our scene has been laid down, it needs to be played out. But how do we do that? It's simple - with the help of DSC, or in easier words, our director.
To 'direct' or 'execute' this scene, we have to push it to the Windows PowerShell DSC Local Configuration Manager (LCM). The LCM is like the shooting location where all our scenes are shot, i.e., the DSC scripts executed. LCM acts as the engine of DSC ensuring all tasks and configurations get actioned as described. Apply the configuration using the below script:
HackerCtrlScene -OutputPath "C:\DSCConfigurations"
Start-DscConfiguration -Wait -Verbose -Path "C:\DSCConfigurations"
Here, -OutputPath
parameter is used to specify the location where the MOF files are stored which are essentially the shooting scripts for each computer (node). The Start-DscConfiguration cmdlet takes these MOF files and starts applying the configuration as directed.
Okay, good. We have our scripts and location set. Let's take this a bit further. Suppose the plots of the movie have a twist and the director needs certain specific software details like the version, vendor name on his machine before the shooting can begin. DSC helps with that too!! Let's see how:
Configuration DirectorMachine {
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node "MovDirectorMachine" {
Package "ScreenplaySoftware" {
Ensure = "Present"
Path = "C:\Files\ScreenplaySoftwareSetup.msi"
Name = "Screenplay Writing Software"
ProductId = "30a5f3a6-38b3-4ee7-b5bc-4330405e90c0"
}
}
}
DirectorMachine
Start-DscConfiguration -Wait -Verbose -Path .\DirectorMachine
In the above code, we instruct the DSC to do a specific setup for the director's machine. Package
is the resource type that allows us to install or uninstall packages on a node. With the Ensure
keyword set to "Present", we make sure the "Screenplay Writing Software" is installed on the director's machine.
In conclusion, DSC is a wonderful tool when you need to manage your infrastructure in an automated and predictable manner. Its movie-like roles and script-based design make sure the necessary Windows features or softwares are present and running as demanded with minimal operator intervention. It's all about writing the perfect script (Configuration) and letting the director (DSC) make the magic happen!
0 Comment
Sign up or Log in to leave a comment