title: How to use public static IP with Azure VM
body: <p>After spending several hours of trying every relevant Azure Power Shell command to assign a virtual public IP to a Azure VM, I am back to square one.  It is amazing that such a basic function cannot be done in Azure.</p>
  
  <p>First, I reserved a static IP.  Then create a vm.  Then tried set-azurestaticvnetip.  It complaint that subnetnames could not be null.  So I created a virtual network in Azure portal and created a subnet.  Then used set-azuresubnet -subnetnames subnet-1. Now it complaint that virtual network name could not be null.  Problem is that none of the commands in the sequence takes -vnetname as a parameter.</p>
  
  <p>I then found that this parameter could be passed in new-azurevm.  So I deleted (what a nice workaround. i am glad I did not spend time configuring software in the vm) the VM and tried to create using this command. (image parameter is not specified in this command. I was entering that on prompt).</p>
  
  <pre><code>new-azurevmconfig -name myvm -instancesize Basic_A2|
  add-azureprovisioningconfig -adminusername "myvmadmin" -windows -password "myvmP123"|
  set-azuresubnet -subnetnames subnet-1 |
  set-azurestaticvnetip -ipaddress 23.101.39.28 |
  new-azurevm -servicename myvm -vnetname mynet –Location "East US" -waitforboot
  </code></pre>
  
  <p>Throws error- </p>
  
  <pre><code>new-azurevm : BadRequest : The static address 23.101.39.28 doesn't belong to the address space defined by the role's subnets.
  </code></pre>
  
  <p>What is wrong here?  Looks to me, these instructions are for assigning a private IP to VM.  It is so easy to assign a static IP to a VM created through Web Role (just put public IP name in the config file, thats it).  How do I assign a Public IP to a VM.  Microsoft documentation merely states that public IP can be used for a VM but did they think everybody would know how to do that?</p>