📜 ⬆️ ⬇️

Quick server setup with PowerShell Desired State Configuration

In many ways, the work of the system administrator does not differ original tasks. Anyway, most tasks are repetitive operations, which are reduced 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 of the tools rushing to help the administrator is the PowerShell Desired State Configuration , which was first introduced in Windows Server 2012 R2. What this is and how it can make the life of an IT specialist easier, using the Desired State Configuration will be discussed in this article.



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 necessary meaning. In this case, we are dealing with an exception. Desired State Configuration is translated as “setting the desired state”, and this transfer reflects the meaning of the technology.

Using PowerShell DSC, you describe how you want your system to look at the end, and then it is automatically configured according to the specified requirements.
')
PowerShell Desired State Configuration is a very powerful tool that can greatly facilitate your system configuration process. Recently on the portal MVA out the course , which tells about all aspects of this technology. And in this article I want to talk about technology and its main advantages.

System requirements


There are no special system requirements for DSC. Desired State Configuration is a Microsoft technology introduced as part of the Windows Management Framework 4.0 and intended 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 in Windows 8.1 and Windows Server 2012 R2 (but for correct operation, update KB28883200 is needed). Also, this Framework is available for Windows 7, Windows Server 2008 R2 and Windows Server 2012 (here you need to remember to install also .NET 4.5). 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 too complicated operation. Nevertheless, we will spend some time to install some missing elements or server components that are necessary for correct operation. If we install on an already used computer, we may encounter various conflicts. 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 NOT ANYTHING! In it, we simply declaratively describe how we want the system to look.

A new Configuration keyword was introduced in the DSC, which describes the main configuration container. In essence, this block is a function. Inside the Onfiguration C box, you can specify the desired settings for each of the computers in your environment.

The block of a particular computer begins with the keyword Node . The following is the name of the target computer, which can be either constant or specified using a variable. Inside the Node block, you already specify the desired setting for your destination computer, using resources.

DSC resources are specialized PowerShell modules with the help of which final configuration of target nodes is performed. Resources are divided into built-in and user. There are only 12 built-in resources. However, you can easily add missing resources if the need arises.

What happens in our script?
We simply describe how our system should look like.

 Ensure = "Present" 

With this command, we make sure that the components we need will be present on the target computer after the configuration has been applied.

The application configuration 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 to the target computer. The name of the MOF file will correspond to the Node value. The file itself will be located in a folder whose name will coincide with the name Configuration . It is important to note here that using MOF files allows you to use 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). The 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 for correctness, 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 of application of the configuration depends on how the current configuration of the computer corresponds to the requirements specified 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 

After launching it, we will start checking whether the current system configuration is the same as the one in the MOF file. If the configurations match, then the value “True” will be returned, otherwise - “False”.

What if we learn about configuration changes? If we use PowerShell DSC, all you have to do is 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 talk about the advantages of the PowerShell Desired State Configuration.

Hope this article was helpful.

You can learn more about how to create your own resources or how to configure the Pull Server for PowerShell DSC from the course on the MVA portal .

Good luck in using PowerShell Desired State Configuration to configure your systems!

useful links


Source: https://habr.com/ru/post/253497/


All Articles