Homepage image
Sample of the Day RSS Feed

Sample download: http://code.msdn.microsoft.com/CSAzureAddRegistryKeysToVMs-75edd269

Today’s code sample demonstrates how to add registry entries to VMs running in Windows Azure programmatically. One of the common asks from developers is the ability to write to registry in Windows Azure. Startup tasks in Windows Azure can help you write to registry.  This sample code will add the registry keys to Azure VM's.

The sample is written by Microsoft Escalation Engineer - Narahari Dogiparthi.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

One of the common asks from developers is the ability to write to registry in Windows Azure. Startup tasks in Windows Azure can help you write to registry.  This sample code will add the registry keys to Azure VM's.

 

Building the Sample

This sample can be run as-is without making any changes to it.

 

Running the Sample

Open the sample on the machine where VS 2010, Windows Azure SDK 1.6 are installed.
 
Right click on the cloud service project i.e. CSAzureAddRegistryKeysToVMs and choose Publish.
 
Follow the steps in publish Wizard and choose subscription details, deployment slots, etc. and enable remote desktop for all roles.
 
After successful publish, login to Azure VM and verify that 3 registry keys(One for string, One for binary value, One for DWORD value) are created under HKEY_LOCAL_MACHINE\System\Test\Test1

 

Using the Code

1) Create sample.reg file with required registry entries. For testing purposes, I’m using sample.reg file with below entries. In real world scenarios, you can create the registry file simply by exporting the section of registry settings you would like to add to Azure VM’s.
 
  Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\System\Test\Test1]
"New Binary Setting"=hex:42,69,6e,61,72,79,20,56,61,6c,75,65
"New DWORD Setting"=dword:00000001
"New String Setting"="Something"

 
This sample file adds values settings named “New Binary Setting”, “New DWORD Setting”, “New String Setting” and configures with above specified values.

2) Add the sample.reg file to web role / worker role project as required.

3) Configure below file properties for sample.reg file , so that it will be copied to bin directory.

Build Action : Content
Copy To Output Directory : Copy Always

4) Create addreg.cmd file with below code.
 
regedit /s sample.reg
exit /b 0

5) Add the addreg.cmd file to Web Role / Worker role as required.

6) Configure below file properties for addreg.cmd file, so that it will be copied to bin directory.
 
Build Action : Content
Copy To Output Directory : Copy Always

7) Finally, define startup task in ServiceDefinition.csdef file by adding following block of configuration under <Webrole> / <WorkerRole> tag

<Startup>   
  <Task commandLine="addreg.cmd" executionContext="elevated" taskType="simple">  </Task>   
</Startup> 

8) Deploy the application to cloud and you should have “New Binary Setting”, “New DWORD Setting”, “New String Setting” under HKEY_LOCAL_MACHINE\System\Test\Test1 path.

 

More Information

Below articles gives you additional details regarding adding, modifying, deleting registry sub keys, .reg file syntax http://en.wikipedia.org/wiki/.reg
 
How to add, modify, or delete registry subkeys and values by using a registration entries (.reg) file http://support.microsoft.com/kb/310516
 
Note: If you want to verify that settings are added properly, enable remote desktop connection and connect to VM to verify the settings. Below article can help you enable remote desktop option to your Windows Azure VM’s
 
How to connect to VM using Remote Desktop(RDP) on Windows Azure (Cloud) http://blogs.msdn.com/b/narahari/archive/2010/12/01/how-to-connect-to-vm-on-windows-azure.aspx