Finding VM IP Address in a Windows Azure startup task
In a Windows Azure startup task, there are a few options for finding a server’s IP address.
One way is to add an environment variable via the csdef as follows:
Then in a startup task you can access the variable like a normal enviroment variable in a powershell or batch script:
That works great on any type of Azure role but requires a change to the csdef. If you know you only need to get the IP for a web role, you can grab the IP from the IIS bindings with powershell.
How this works is by grabbing the bindings for your site from IIS via the appcmd exe. Next feed the bindings string into a regex and extract the IP Address.
Both options work, but my preference is to use the second when I’m in a web role because it doesn’t require any changes to the .csdef.