In this post, I want to show how to use Windows PowerShell Desired State Configuration (DSC) and the VM Agent to perform various configuration management tasks like remote debugging and at the end, I will show how to use variables in a deployment template.
Automate configuration management by using PowerShell Desired State Configuration (DSC) or VM Agent (custom script extensions)
The VM Agent gets installed automatically when creating a new VM. It is a lightweight process which can install and configure VM extensions. These extensions can be added through the portal, with PowerShell cmdlets or through the Azure Cross Platform Command Line Interface (Azure CLI)
Popular VM extensions are:
- Octopus Deploy Tentacle Agent
- Docker extension
- Chef extension
Configure VMs with Custom Script Extension
Custom Script Extensions allow you to automatically download files from Azure store and run PowerShell or Shell scripts to copy files or configure VMs.
- Open the blade of your Windows Server
- Under the Settings section, select Extensions
- On the Extensions blade, click + Add on the top
- This opens a new blade with all available extensions. Select Custom Script Extension and press Create
- On the install extension blade upload a PowerShell script file which you want to run when the VM starts. The PowerShell script contains only one line to copy a file from C:\ to the desktop. The entire command is: Copy-Item “FilePath” -Destination “DestinationPath”. Before I upload the script, I create the file under C:\.
- Click OK and the script will be downloaded on the Server.
- When you start your VM, the VM executes the script which opens a Windows Explorer window. </ol> </li> </ol>
Using PowerShell Desired State Configuration (DSC)
DSC is a management platform which was introduced with PowerShell 4.0. It has an easy, declarative syntax which simplifies configuration and management tasks. With DSC it is possible to describe what application resources you want to add or remove based on the current state of a server node. A DSC describes the state of one or more resources. These resources can be the Registry or the filesystem. Tasks you can achieve with a DSC script are:
- Manage server roles and Windows features
- Copy files and folders
- Deploy software
- Run PowerShell scripts
DSC extends PowerShell (4.0 and upwards) with a Configuration keyword which is used to express the desired state of one or more target nodes.
Many resources are already defined and exposed to DSC. Additionally, it is possible to implement custom resources by creating a PowerShell module. This module includes a MOF (Managed Object Format) file, a script module, and a module manifest.
Local Configuration Manager
The Local Configuration Manager runs on all target notes and is the engine of DSC. It enables you to do the following tasks:
- Pushing configurations to bootstrap a target node
- Pulling configuration from a specified location to bootstrap or update a target node
The documentation for the Local Configuration Manager can be found here.
Configure VMs with DSC
To configure a VM using DSC, you have to create a PowerShell script which describes the desired configuration state of this VM. This script selects the resources which need to be configured and provides the desired settings. After the script is created, there are several methods to run the script when the VM starts.
Creating a configuration script
To create a configuration script, create a PowerShell file and include all nodes which you want to configure. If you want to copy files, make sure that the file exists at the source and that the destination also exists.
Deploy a DSC script
Before you can deploy your DSC script, you have to pack it in a zip file. To do that open PowerShell and enter Publish-AzureVMDscConfiguration Filepath -ConfigurationArchivePath DestinationPath.
Publish-AzureVMDscConfiguration .\IIS.ps1 -ConfigurationArchivePath .\IIS.ps1.zip
After the script is prepared, follow these steps to deploy it to your VM:
- In the Azure Portal open your VM
- Click on Extensions under the settings menu.
- On the Extensions blade click + Add, then select PowerShell Desired State Configuration and click Create.
- On the Install extension blade, upload your zip File, enter the full name for your script and enter the desired version of the DSC extension.
- Click OK to deploy your script.
Enable remote debugging
You can use Visual Studio 2015 or 2017 to debug applications which run on your Windows VM. To do that following these steps:
- Open the Cloud Explorer in Visual Studio.
- Expand the node of your subscription and then the Virtual Machines node.
- Right-click on the VM you want to debug and select Enable Debugging. This installs the debugging extension on the VM and takes a couple minutes.
- After the extension is installed, right-click on the VM and select Attach Debugger. This opens a new window with a list of all available processes.
- Select the process you want to debug and click Attach. The probably most useful process is w3.wp.exe. This process only appears if you have a web application running on your server though.
Implement VM template variables to configure VMs
When you create a new resource, for example, a VM from the marketplace, it creates a JSON template file. You can write this template by yourself or you can download the template from an existing VM and modify it. Variables can be used to simplify your template which makes it easier to read and easier to change. They also help you with your configuration management since you only have to change one value to change the configuration for your whole template. In this section, I will edit an existing template to show, how variables can be used to simplify a template.
Edit an existing template from the Azure Marketplace
- In the Azure Portal go to the Marketplace.
- Enter template in the search, then select Template deployment and click Create.
- On the custom deployment blade select Create a Windows virtual machine. This opens the Deploy a simple Windows VM blade.
- Click Edit template to open the template.
- When you scroll down a bit, you will find the “variables” section. All variables are configured in this section. Change any of the values or add your own variables.
- To use the variable use “[variables(‘yourVariableName’)]”.
In this post, I talked about what Desired State Configuration is and what you can do with it. Then, I showed how to enable remote debugging using Visual Studio 2015 or 2017 and in the last section, I explained how variables in templates work and how they can be used to simplify the template.
For more information about the 70-532 exam get the Exam Ref book from Microsoft and continue reading my blog posts. I am covering all topics needed to pass the exam. You can find an overview of all posts related to the 70-532 exam here.