How to define a unique identifier for each Windows Azure Virtual Machine


I have some Virtual machine running with same configuration. On those VMs I have installed my application.

I want to set some custom unique identifier in my VM , so that I know that if particular (from my application) request is coming from which VM that request is coming(or that application is running in which VM). As same application is running inside all VM, so to differentiate each request , I want to setup some unique custom identifier at the time of creation of Virtual machine.

I want to setup some system properties/Environment variable at the time of creation of virtual machine.

I am using Windows Azure Rest API and Java to create and manage Virtual machine on Windows Azure Portal. I want to achieve this by using Java programing language.

Please let me know if you have nay information about same.

Each instance should have a different SID because the machines are sysprepped with the generalize option (the same applies when you want to provide a custom image). You can get the SID using the code in this question, and use that as identifier:

        string domainName;
        Helper.SID_NAME_USE accountType;
        SecurityIdentifier machineSID =
                Helper.LookupAccountName("", Environment.MachineName, out domainName, out accountType);

Note: even though it says domainName in the code, your machine doesn't need to be in a domain for this to work.

To set a "custom identifier" on your VM you can use environment variables. Since you're using Java the easiest way to set these is probably the setx.exe command line tool (download). You can call the tool like this:

setx.exe VmIdentifier 123 -m

Then you should be able to read it using System:


It's not really clear to me if you're using Virtual Machines (IaaS) or Cloud Services (PaaS). If you're using Virtual Machines, it will be up to you to deploy the application, and while doing so, you can also execute setx.exe.

Now, if you're using Cloud Services, you actually upload a package which gets deployed on your instances. And you get support for startup tasks (which run before your application starts): How to Define Startup Tasks for a Role. You can use a startup tasks to run setx.exe, but make sure you run it with executionContext set to elevated .