Thursday, May 19, 2011

Windows Azure Powershell – Find IP of Azure Role Instance VM

I was unknown about power of Powershell, until a requirement to find IP of virtual machine of Azure role came to me.
Alright here we go!!
Problem statement – To find IP of Azure hosted role instance machine and use it on another role instance present in the same Azure hosted service.
Applicable technology – windows Azure SDK 2.3

Let’s say I have cloud service project in VS2012 with 2 roles in it. As shown below –


I have published this cloud service project in Azure cloud service named as "findipdemo" .
Now I want to find IP of “WebRole1” and "WorkerRole1" role only using powershell. Following is the code which you can use to find IP of azure role VM by specifying the role name.



First you need open the powershell for windows Azure on your local machine and then type following code to add azure account in current powershell window – 

Add-AzureAccount

This will open a pop up and you need to enter the Microsoft credentials to add account. Then you need to select the subscription where your cloud service resides. Therefore use following command – 

Select-AzureSubscription

This will ask for entering the name of subscription. The name of subscription can be obtained from azure management portal as shown below –



Then you need to fire following command in powershell to get ip of each role present in cloud service on azure – 


Get-AzureRole -ServiceName “cloudServiceName” -Slot “Your Slot - productionORStaging” -InstanceDetails -rolename “yourRoleName”

This way you can get the IP of role VM similar to shown below –



Similarly if you wish to have VIP of cloud service then use following command –

Get-AzureRole -ServiceName “cloudServiceName” -Slot “YourSlot - productionOrStaging” -InstanceDetails -rolename “YourRoleName” | select {$_.InstanceEndpoints[0].VIP } 




Cheers…
Happy coding!!

No comments:

Post a Comment