Search code examples
azurevirtual-machineazure-marketplace

Auto-Starting an Application on Microsoft VM Obtained from Azure Marketplace


I have deployed a Windows Server 2016 VM image to the Microsoft Azure Marketplace. The image contains several startup scripts and programs that need to be executed when a customer runs a VM created from the image obtained from the Marketplace.

How can I cause those scripts and programs to run automatically every time the customer reboots the VM?

Some background... (1) I have created a task using the Windows task manager and this works if I can guarantee that the customer creates an admin user with a user id and password identical to the one I used when creating the original VM, otherwise the existing scheduled task fails because it can't find the original user id.

(2) Some of my components are already Windows services but I still need to run a script to perform a number of VM-specific installation steps. This doesn't sound like the role of a service. Also, Windows services have a startup timeout value that is too low for the installation steps that need to be performed.

Any help would be appreciated.


Solution

  • You have a couple different options since you are in Azure, however this is the one I recommend.

    Use the SetupComplete.cmd or Unattend.xml file process - This is built into Windows so nothing special needs to occur. You will basically create your own custom SetupComplete script that has the logic that you need to execute to install y our applications and reconfigure your windows server to run as the new user that is created when the image is used to create a new VM. Note that in Azure, Microsfot is using the SetupComplete, so you should call yours SetupComplete2.cmd. I have used this technique to to run custom PowerShell scripts. So the SetupComplete2.cmd will call the PowerShell script. You can have as many of these as you like, just name them with a number in sequence that you want these two run. The scripts will need to be placed in the C:\Windows\OEM folder.

    You could additionally use the Unattend.xml too, however that is a whole different topic I am not as familiar using.

    Lastly, you could potentially be able to use Custom Script extensions in Azure, but I don't know the the process for forcing those extensions as part of a marketplace image.

    Oh, one last thing, I am assuming you are running SysPrep on that image that you are creating. If not, that would also be a good thing to do.