How to assign Public IP Address to ARM based Azure VMs using PowerShell

How to assign Public IP Address to ARM based Azure VMs using PowerShell

How to assign Public IP Address to ARM based Azure VMs using PowerShell

I was working on engagement where I deployed 100+ virtual machines using ARM templates (JSON) and created those many Public IP addresses. Now I had to assign Public IP addresses to these Virtual Machines or to Virtual Network Adapter. Doing this on the Azure Portal (http://portal.azure.com) manually for these many VMs will never be easy.

Below is the PowerShell commands to assign Public IPs to Azure Virtual Machines –

Create New Azure Resource Manager Public IP Address

New-AzureRMPublicIPAddress -Name testIP -ResourceGroupName testRG -AllocationMethod Dynamic -Location "East Asia" -DomainNameLabel testip

Query Network card

$nic = Get-AzureRMNetworkAdapter -ResourceGroupName testRG -Name testnic

Query Public IP Address

$pip = Get-AzureRMPublicIPAddress -ResourceGroupName testRG -Name testip

Setting Public IP to Network Card

$nic.IPConfiguration[0].PublicIPAddress=$pip

Save the Configuration

Set-AzureRmNetworkInterface -NetworkInterface $nic

If you have multiple VMs then you could create a loop and assign the Public IP addresses to the machines

for ($i = 0; $i -le 100; $i++)
{
  $nic= Get-AzureRMNetworkInterface -ResourceGroupName testRG -Name myVM$($i)
  $pip= Get-AzureRMPublicIPAddress -ResourceGroupName testRG -Name mypip$($i)
  $nic.IPConfigurations[0].PublicIPAddress = $pip
  $Set-AzureRMNetworkInterface -NetworkInterface $nic
}

This assumes that your VMs are named in sequential order (myVM0… myVM100) and same with Public IP Address. If they are not named on order like mentioned above then you could use Excel (CSV file) and use it for mapping.

Hope this helps.

 

 

About The Author

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *