Powershell DSC is a great configuration tool for setting up your virtual machine, and the nice people at Microsoft have given us a Powershell DSC extension for our Azure Virtual Machines. Let’s go through the different ways of setting this up.
There are 3 different methods for adding the Powershell DSC accommodation
Prerequisites
1. Create a Virtual Machine
You need an Azure Windows Virtual Machine up and deployed. If you don’t have one you can use the ARM code from a previous blog post to create one located here on github
2. Create your PowerShell DSC configuration
Open up a new file in your favourite editor, create a new file called ExampleDSC.ps1 and copy in the following code:
Configuration ExampleDSC
{
Node localhost
{
File CreateFolder
{
Type = 'Directory'
DestinationPath = 'C:\NewFolder'
Ensure = "Present"
}
}
}
Now zip up this file and name it ExampleDSC.zip
1. The Azure Portal
Load up the Azure Portal and select the Virtual Machine that you want to add the DSC configuration to.
Click on Extensions down the left menu
Click on Add at the top left of the extensions tab
Click on PowerShell Desired State Configuration
Click Create within the extension
The DSC extension configuration screen is now displayed
- We’re going to select our DSC configuration file that we created above, Add the Module-qualified Name of the configuration, select the latest WMF version, Enable Data Collection and choose version 2.26 of the DSC extension (the current latest).
Now click ok
You will now see the Deployment in progress Notification in your top right of the portal in the notifications tab.
The installation of the extension took 10 minutes 35 seconds
2. Powershell
$resourceGroup = 'test-infra'
$vmName = 'testvm1'
$PublicSettings = '{
"ModulesURL": "https://github.com/Owen-Davies/Azure-VM-WMF-Speed-Test/raw/master/ExampleDSC.zip",
"configurationFunction": "ExampleDSC.ps1\\ExampleDSC
}'
Set-AzureRmVMExtension -ExtensionName "DSC" -ResourceGroupName $resourceGroup -VMName $vmName `
-Publisher "Microsoft.Powershell" -ExtensionType "DSC" -TypeHandlerVersion 2.26 `
-SettingString $PublicSettings -Location $location
Time taken: 10 minutes
3. ARM Template
Create a file called add-extension.json and copy the following code into to it:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vmName": {
"type": "string",
"defaultValue": "testvm1",
"metadata": {
"description": "Name for our Virtual Machine"
}
}
},
"variables": {
},
"resources": [
{
"apiVersion": "2015-06-15",
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(parameters('vmName'),'/Microsoft.Powershell.DSC')]",
"location": "[resourceGroup().location]",
"dependsOn": ["[concat(parameters('vmName'))]"],
"properties": {
"publisher": "Microsoft.Powershell",
"type": "DSC",
"typeHandlerVersion": "2.26",
"autoUpgradeMinorVersion": true,
"forceUpdateTag": "1.1",
"settings": {
"modulesUrl": "https://github.com/Owen-Davies/Azure-VM-WMF-Speed-Test/raw/master/ExampleDSC.zip",
"wmfVersion": "latest",
"ConfigurationFunction": "ExampleDSC.ps1\\ExampleDSC"
}
}
}
],
"outputs": {
}
}
$resourceGroup = 'test-infra'
New-AzureRmResourceGroupDeployment -Name DeployDSCToVM -ResourceGroupName $resourceGroup -TemplateFile add-extension.json
This took 13 minutes 19 seconds to run.
Conclusion
There we have it, three different ways to add your DSC configuration to an existing Virtual Machine.
I would definitely choose to rarely use the portal. In fact as much as possible I would try to not use the portal. You should be scripting everything you do. Not just running script in a powershell window, but actually writing a repeatable powershell script and saving it, and running THAT.
ARM Templates are equally useful or sometimes WAY more useful. To have a folder with the server name that you are managing, and have a bunch of configuration files to get an exact copy of that server is immensely powerful. Maybe you want to test a new patch, or run an exact same instance in a UAT environment, bang easy.