How to alter the public IP address of an Azure virtual machine


Summary: Change the public IP address in Azure Resource Manager by using Windows PowerShell.

Honorary Scripting Guy, Will Anderson, shares a personal challenge that he encountered when working with Azure and public IP addresses. He also shares the solution with the rest of us!

Take it away, Will!

Recently, I incorrectly configured an Azure Resource Manager virtual machine (VM) in my lab environment and needed to make some changes to the public IP settings. A brief look around the Internet came up empty, so I thought I’d share the challenge and the solution to this puzzle with you.

The challenge

If we take a look at Set-AzureRmPublicIpAddress, it uses only one parameter: -PublicIPAddress. To actually change the properties in the object, however, we need to call them. How do we do that? It depends on the property in question, so I’ll give you an example of two different properties that we’re going to change on our target VM. In this example, we’re going to change our public IP allocation from Dynamic to Static, and we’re going to give our PublicIP a friendly name and a fully qualified domain name (FQDN).

Data formatting or using an existing example

We can look up an existing PublicIP configuration by doing the following:

$ExResGrp = Get-AzureRmResourceGroup -Name 'lwindsc'

$PubIP = (Get-AzureRmPublicIpAddress -ResourceGroupName $ExResGrp.ResourceGroupName).where({$PSItem.Name -eq 'lwindsctgtwestuspubip'})

And as we can see, we have a configuration to look at:

Screenshot of results that show an existing PublicIP configuration.

The PublicIpAllocationMethod, where we define our configuration as Static or Dynamic, is a simple string, which is easy enough to pass along. But, if you notice, the DNSSettings are displayed in a hashtable. So, let’s construct our changes. First, we cast our target PublicIP configuration object to a variable:

$TgtResGrp = Get-AzureRmResourceGroup -Name 'lwinpubip'

$PubIP = Get-AzureRmPublicIpAddress -ResourceGroupName $TgtResGrp.ResourceGroupName

If we call the object, we’ll see that the PublicIpAllocationMethod is set to Dynamic, and the DnsSettings property is null

Screenshot that shows that the PublicIpAllocationMethod is Dynamic and the DnsSettings property is null.

Make the change

Now we call the properties that we want to modify and the values that we want to input.

$PubIp.PublicIpAllocationMethod = 'Static'
$PubIP.DnsSettings = @{

'DomainNameLabel' = ($TgtResGrp.ResourceGroupName + $TgtResGrp.Location + 'pubip')
'Fqdn' = ($TgtResGrp.ResourceGroupName + '.westus.cloudapp.azure.com')

}

If we look at our stored object, we can see the changes that we’ve made:

Screenshot that shows that the PublicIpAllocationMethod is changed to Static and the DnsSettings property is no longer null.

Now we commit the changes to Azure by passing our object back with Set-AzureRmPublicIpAddress.

$PubIP | Set-AzureRmPublicIpAddress

Screenshot that shows results after pass the object back with Set-AzureRmPublicIpAddress.

And now our system is accessible remotely by a friendly name!

Thanks, Will, for sharing that with the community! I’m certain you’ve made somebody’s day!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Until then, always remember that with Great PowerShell comes Great Responsibility.

Will Anderson
Honorary Scripting Guy
Cloud and Datacenter Management MVP

Comments (0)

Skip to main content