Question

Is there a way to add a (parametrized) Startup task to a Windows Azure Virtual Machine through the API? I need to execute a cmdlet after the machine has been started and the code depends on two parameters that will be different for each machine. I know this could be easily achieved for a Web/Worker role, but could it be done for Virtual Machines, as well?

Était-ce utile?

La solution 2

No. currently there is no such feature provided out of the box.

However, given you will deal with VM anyway, you can create an image of your own. You can register a "Startup Task" in RunOnce registry key. And sysprep the OS with this settings.

This way you will basically have a startup task which will be executed when your machine boots for the first time and will not be executed on consequent VM restarts.

Taking parameters into the code for VM is not as easy for Web/Worker Role. For anything you want you have to query the Azure Management API directly. The only properties you can get from code running on an Azure VM are basically the normal OS properties - i.e. host name, host IP Address. You don't even know your cloud service name, nor your Virtual IP Address (this can be discovered via services as whatismyip.net or similar). So my approach would be to put parameters into an Azure Table Storage and use Machine Name as rowKey. So I can store any VM specific values based on VM Name. And my "Startup" task would query the Table storage, providing my host name as rowKey (and some common pattern for Partition Key), so it gets all required settings.

Autres conseils

For first-time runs of a VM, you can inject a startup task via CustomData. This works in both Linux and Windows VMs. You'll just need to properly base-64-encode your file (whether it's text or binary) based on the REST API docs.

CustomData is dropped into a file in a specific location, and you can have code that looks for this file, taking some type of startup action as appropriate:

  • Windows: %SYSTEMDRIVE%\AzureData\CustomData.bin
  • Linux: /var/lib/waagent/CustomData

Note: This will be added to the CLI as well (the pull request is already available - not sure if it's in the latest build.

EDIT Yes, customdata is now part of the Azure CLI, as a parameter to azure vm create, so no need to mess with base-64 encoding on your own :

enter image description here

With IaaS Management Studio you can set a startup script that will run when your VM boot. In summary, it activates remote powershell and run your script remotely when it detects the powershell port is open.

I am the developer of this tool, but I don't really get what you mean by "parametized", in other words you want your script to have access to the VM info ?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top