![](http://habrastorage.org/getpro/habr/avatars/ab4/bc3/dd2/ab4bc3dd2ace05229d18259e72783fb9.jpg)
Quick server setup with PowerShell Desired State Configuration
In many ways, the work of the system administrator does not differ in the originality of the tasks. One way or another, most tasks are repetitive operations that come down to creating, deleting, changing settings, installing and configuring roles and system components. Absolutely natural is the desire to automate such tasks as much as possible. One tool that hurries to help the administrator is the PowerShell Desired State Configuration , which was first introduced in Windows Server 2012 R2. We will talk about what it is and how it can make life easier for an IT professional using Desired State Configuration in this article.
![](https://habrastorage.org/files/869/fad/1b2/869fad1b22924c6aa168093f16e66121.png)
It is logical to start with what PowerShell Desired State Configuration is. In most cases, I do not like to translate English terms, because not always translated version reflects the desired meaning. In this case, we are dealing with an exception. Desired State Configuration translates as “setting the desired state”, and this translation is the best possible reflection of the meaning of the technology.
Using PowerShell DSC, you describe how you want your system to look in the end, and then it is automatically configured in accordance with the specified requirements.
PowerShell Desired State Configuration is a very powerful tool that can greatly facilitate your system configuration process. Recently, the MVA portal released a course, which talks about all aspects of this technology. And in this article I want to talk about the technology and its main advantages.
DSC does not have any special system requirements. Desired State Configuration is a Microsoft technology introduced as part of the Windows Management Framework 4.0 and designed for declarative system configuration. Accordingly, in order for Desired State Configuration to work correctly, you need to install Windows Management Framework 4.0 or higher. WMF 4.0 is preinstalled on Windows 8.1 and Windows Server 2012 R2 (but KB28883200 is needed to work correctly). Also, this Framework is available for Windows 7, Windows Server 2008 R2 and Windows Server 2012 (here you must remember to install .NET 4.5 as well). Due to the fact that you can upgrade for free from Windows 8 to Windows 8.1, WMF 4 for Windows 8 is not available.
Now let's talk about how the Desired State Configuration works. Let's imagine the following task: we need to deploy a website on our server. To do this, you need to install IIS, ASP .NET, as well as the site content itself. This is not a very complicated operation. However, we will spend some time to install any missing elements or server components that are necessary for the correct operation. If we install on an already used computer, you may encounter various conflicts. Even more confusion will arise if we need to configure several servers with the same configuration at once.
Using Desired State Configuration solves this problem. Next, I will give a configuration script with which we will configure.
On the one hand, PowerShell DSC starts with a configuration script. On the other hand, this script does absolutely NOTHING! In it, we simply declaratively describe how we want the system to look.
A new Configuration keyword has been introduced in the DSC that describes the main configuration container. In essence, this block is a function. Inside the block C onfiguration you can specify the desired settings for each of the computers that are in your environment.
A specific computer block starts with the Node keyword . The following is the name of the target computer, which can be either constant or set using a variable. Inside a Node BlockYou are already specifying the desired setting for your destination computer using resources.
DSC resources are specialized PowerShell modules that are used to finalize target nodes. Resources are divided into built-in and user. There are only 12 built-in resources. Nevertheless, it is easy to add the missing resources if such a need arises.
What happens in our script?
We just describe how our system should look.
Using this command, we make sure that the components we need will be present on the destination computer after the configuration is applied.
The configuration application is as follows:
![](http://litehtmlconv.azurewebsites.net/api/pics/1zy_l8wum-tmnaasurgp-ng4_)
After running the configuration script, a MOF file (or Managed Object Format file) is created. This is a text file that contains all the configuration requirements that are subsequently applied on the target computer. The name of the MOF file will match the Node value . The file itself will be located in a folder whose name will match the name Configuration . It is important to note here that the use of MOF files allows the use of DSC not only to configure computers running Windows, but also running Linux.
Next, you need to transfer the MOF file to the target computer (on which we want to deploy the site). Configuration is applied in two ways: using the Push method (the configuration script must be transferred to the destination computer manually) or the Pull method (a Pull Server is created that periodically checks the configuration is correct, and if the client is configured incorrectly, the Pull Server sends the required settings to it). The configuration is applied using the following cmdlet:
Using the –Path parameter, specify the path to the MOF file. The time it takes to apply the configuration depends on how much the current configuration of the computer matches the requirements indicated in the MOF file.
After applying the configuration, the PowerShell DSC feature does not end there. After all, often we need to determine whether there have been any changes in the settings. You can do this using the cmdlet:
By launching it, we will run a check: does the current system configuration match the one that is written in the MOF file. If the configurations match, then “True” will be returned, otherwise “False”.
What if we learn about a configuration change? If we use PowerShell DSC, just run the cmdlet again:
And all the missing items will be restored.
Once again I’ll say what the benefits of the PowerShell Desired State Configuration are.
I hope this article was helpful to you.
You can learn more about how to create your own resources or how to configure Pull Server for PowerShell DSC from the course on the MVA portal .
Good luck with the PowerShell Desired State Configuration to configure your systems!
![](https://habrastorage.org/files/869/fad/1b2/869fad1b22924c6aa168093f16e66121.png)
It is logical to start with what PowerShell Desired State Configuration is. In most cases, I do not like to translate English terms, because not always translated version reflects the desired meaning. In this case, we are dealing with an exception. Desired State Configuration translates as “setting the desired state”, and this translation is the best possible reflection of the meaning of the technology.
Using PowerShell DSC, you describe how you want your system to look in the end, and then it is automatically configured in accordance with the specified requirements.
PowerShell Desired State Configuration is a very powerful tool that can greatly facilitate your system configuration process. Recently, the MVA portal released a course, which talks about all aspects of this technology. And in this article I want to talk about the technology and its main advantages.
System requirements
DSC does not have any special system requirements. Desired State Configuration is a Microsoft technology introduced as part of the Windows Management Framework 4.0 and designed for declarative system configuration. Accordingly, in order for Desired State Configuration to work correctly, you need to install Windows Management Framework 4.0 or higher. WMF 4.0 is preinstalled on Windows 8.1 and Windows Server 2012 R2 (but KB28883200 is needed to work correctly). Also, this Framework is available for Windows 7, Windows Server 2008 R2 and Windows Server 2012 (here you must remember to install .NET 4.5 as well). Due to the fact that you can upgrade for free from Windows 8 to Windows 8.1, WMF 4 for Windows 8 is not available.
Configuration and its application
Now let's talk about how the Desired State Configuration works. Let's imagine the following task: we need to deploy a website on our server. To do this, you need to install IIS, ASP .NET, as well as the site content itself. This is not a very complicated operation. However, we will spend some time to install any missing elements or server components that are necessary for the correct operation. If we install on an already used computer, you may encounter various conflicts. Even more confusion will arise if we need to configure several servers with the same configuration at once.
Using Desired State Configuration solves this problem. Next, I will give a configuration script with which we will configure.
Configuration IISWebsite
{
param(
$NodeName
)
Node $NodeName
{
WindowsFeature IIS
{
Ensure = "Present"
Name = "Web-Server"
}
WindowsFeature ASP
{
Ensure = "Present"
Name = "Web-Asp-Net45"
}
File WebContent
{
Ensure = "Present"
Type = "Directory"
SourcePath = "C:\Content\BakeryWebsite"
DestinationPath = "C:\inetpub\wwwroot\"
Recurse = $true
}
}
} IISWebsite -NodeName "DSC01"
On the one hand, PowerShell DSC starts with a configuration script. On the other hand, this script does absolutely NOTHING! In it, we simply declaratively describe how we want the system to look.
A new Configuration keyword has been introduced in the DSC that describes the main configuration container. In essence, this block is a function. Inside the block C onfiguration you can specify the desired settings for each of the computers that are in your environment.
A specific computer block starts with the Node keyword . The following is the name of the target computer, which can be either constant or set using a variable. Inside a Node BlockYou are already specifying the desired setting for your destination computer using resources.
DSC resources are specialized PowerShell modules that are used to finalize target nodes. Resources are divided into built-in and user. There are only 12 built-in resources. Nevertheless, it is easy to add the missing resources if such a need arises.
What happens in our script?
We just describe how our system should look.
Ensure = "Present"
Using this command, we make sure that the components we need will be present on the destination computer after the configuration is applied.
The configuration application is as follows:
After running the configuration script, a MOF file (or Managed Object Format file) is created. This is a text file that contains all the configuration requirements that are subsequently applied on the target computer. The name of the MOF file will match the Node value . The file itself will be located in a folder whose name will match the name Configuration . It is important to note here that the use of MOF files allows the use of DSC not only to configure computers running Windows, but also running Linux.
Next, you need to transfer the MOF file to the target computer (on which we want to deploy the site). Configuration is applied in two ways: using the Push method (the configuration script must be transferred to the destination computer manually) or the Pull method (a Pull Server is created that periodically checks the configuration is correct, and if the client is configured incorrectly, the Pull Server sends the required settings to it). The configuration is applied using the following cmdlet:
Start-DscConfiguration –Path .\IISWebsite –Wait –Verbose
Using the –Path parameter, specify the path to the MOF file. The time it takes to apply the configuration depends on how much the current configuration of the computer matches the requirements indicated in the MOF file.
After applying the configuration, the PowerShell DSC feature does not end there. After all, often we need to determine whether there have been any changes in the settings. You can do this using the cmdlet:
Test-DscConfiguration –CimSession $session
By launching it, we will run a check: does the current system configuration match the one that is written in the MOF file. If the configurations match, then “True” will be returned, otherwise “False”.
What if we learn about a configuration change? If we use PowerShell DSC, just run the cmdlet again:
Start-DscConfiguration –Path .\IISWebsite –Wait –Verbose
And all the missing items will be restored.
Benefits of Desired State Configuration
Once again I’ll say what the benefits of the PowerShell Desired State Configuration are.
- Using PowerShell DSC allows you to fine tune the destination computer without any additional checks. We just describe how we want the system to look; everything else is done automatically.
- PowerShell DSC allows you to track possible changes to settings and quickly fix them, again, without additional checks.
- Using PowerShell DSC, you can configure multiple computers simultaneously and equally.
I hope this article was helpful to you.
You can learn more about how to create your own resources or how to configure Pull Server for PowerShell DSC from the course on the MVA portal .
Good luck with the PowerShell Desired State Configuration to configure your systems!
useful links
- Try Azure for 30 days for free!
- Learn Microsoft Virtual Academy courses
- PowerShell Desired State Configuration
- Corporate devices. How to protect data and applications
- Corporate devices. How to manage hybrid credentials
- Computer Security Basics
- Basics of building a domain network
- Upgrading your organization’s infrastructure with Windows Server 2012 R2
- Windows Server 2012 R2 Licensing
- Download a trial version of Windows Server 2012 R2
- Download Free or Trial Visual Studio