How to Modify Registry keys in Windows Azure Virtual Machine from a web or worker role?

If you have a requirement and decided to modify VM registry keys, you have two options:

  1. Do it from a Standalone Startup task

    1. This modification will be completed even before your role start
    2. Be sure to run the startup task as in elevated mode.
    3. You just need to use standard Windows API to access the VM Registry and make modification
    4. You can update the registry the same way you do in any Windows command line application
    5. You can also write a small code to update the Azure Storage with registry update status to be sure your update went well.
    6. Sometime, you may needs to modify the registry and then reboot the machine to make it accessible to role, which can be done here
  2. Do it in your Role OnStart() function

    1. This will be done the same way
    2. Most of the registry changes will be available after the role start
    3. One advantage I see here is that you can generate a role environment change event, and perform specific task if needed
    4. Restart role can be done here or if you would need to reboot the VM , you can do it
    5. Code will be same as any standard Windows API to change the registry