Automating VM Customization tasks using Custom Script Extension

4월 24, 2014에 게시됨

Senior Program Manager, Microsoft Azure
2/27/2015 Update : For code samples and latest documentation, please check here.   One of the VM Extensions that was launched during /build conference is an extension called ‘Custom Script Extension’ which was released with PowerShell support. If this is the first time you are visiting these blogs, you might want to check the previous blogs here and here to get an overview of the VM Agent and Extensions. So, what does this extension do and how is it different from Remote PowerShell or any other existing remote execution tools?  In a nutshell, Custom Script Extension can automatically download scripts and files from Azure Storage and launch a PowerShell script on the VM which in turn can install additional software components. And just like with any other VM Extension, this can be added during VM creation or after the VM has been running. Sounds simple, yet very useful! Pre-Requisites for running PowerShell Scripts using this Extension:
  • Install Azure PowerShell Cmdlets V0.8.0 or above from here.
  • If the scripts will be run on an existing VM, make sure VM Agent is enabled on the VM,  if not follow this blog post to install one.
  • Upload the scripts that you want to run on the VM to Azure Storage. The scripts can come from a single or multiple storage containers.
  • The script should be authored in such a way that the entry script which is launched by the extension in turn launches other scripts.
Now that you have the necessary pre-requisites, let’s walk through a few use cases to show how these cmdlets can be used. The current version of this extension targets PowerShell cmdlets only but in future this can be expanded to other scripts.

Use Case 1:  Uploading files to a container in the default account.

If you have your scripts in the storage container of the default account of your subscription, then the cmdlet snippet below shows how you can run them on the VM. The ContainerName in the sample below is where you upload the scripts to. The default storage account can be verified by using the cmdlet ‘Get-AzureSubscription –Default’ Note: This use case creates a new VM but the same operations can be done on an existing VM as well.

Use Case 2:  Using non-default storage accounts.

This use case shows how to use a non-default storage either within the same subscription or in a different subscription for uploading scripts/files. Here we’ll use an existing VM but the same operations can be done while creating a new VM.

Use Case 3: Uploading scripts to multiple containers across different storage accounts.

If the script files are stored across multiple containers, then currently to run those scripts, you have to provide the full SAS URL of these files. SAS URLs generated can be generated by using a tool like Azure Storage Explorer. Hopefully these cmdlets will help you automate some of your VM Customization tasks. We’d love to hear from you on what other capabilities you’d like to see in this extension in its upcoming releases.  Please post your comments/questions to here. Update : Once the scripts are upload the script execution status can be viewed by using the following cmdlet:
// Get the VM status $x = get-azurevm -ServiceName $svcName -Name $name //Parse the output to retrieve the extension status, in the example below CustomScript Extension is 2nd in the list of extensions added. $x.ResourceExtensionStatusList[1].ExtensionSettingStatus.SubStatusList