Change Allocation Method for Azure Network Interface

Microsoft Azure Portal - Network Interface PropertiesHave you already provisioned a Microsoft Azure Virtual Machine, with a dynamic private IP address? Do you want to change that dynamically provisioned IP address to a static IP address? If so, you’re in luck! This process is relatively easy using the Microsoft Azure PowerShell module.

Install PowerShell 5.0

First, make sure you’re running PowerShell 5.0. Open a new PowerShell session and run $PSVersionTable, and then inspect the major PSVersion that you’re running. If it starts with a 5, then you’re good to go! The reason we want to use PowerShell 5.0 is because we can easily install PowerShell modules from the PowerShell Gallery! The PowerShellGet module exposes this functionality.

Windows 10 includes PowerShell 5.0 out of the box, so you don’t need to install it. If you’re running a down-level operating system, you can install the Windows Management Framework (WMF) 5 package. After installing WMF 5, you’ll need to restart your system.

Install the Azure Resource Manager (ARM) PowerShell module

Once you’ve verified that you have PowerShell 5.0 installed, go ahead and install the Azure Resource Manager (ARM) PowerShell module. The ARM PowerShell module enables us to interact with our cloud resources inside our Microsoft Azure subscription (account) using user-friendly PowerShell commands! Installing the ARM PowerShell module from the PowerShell Gallery is incredibly easy. Just run the following command:

Install-Module -Name AzureRM -Scope CurrentUser -Force

This command will take a few minutes to install the ARM PowerShell module. After installing the module, you can validate its presence on your local system by running this command:

Get-Module -ListAvailable -Name Azure*

The ARM PowerShell module actually includes many child modules, that target each ARM Resource Provider, so you should see quite a few modules listed from the above command.

Authenticate to Azure

Now that we’ve installed the ARM PowerShell module, we need to authenticate to Azure, in order to manage our cloud resources. Authentication to Azure, from PowerShell, is a single command:

Add-AzureRmAccount -Credential (Get-Credential)

IMPORTANT: If you’re logging in with a Microsoft Account, instead of an Azure Active Directory (AAD) account, then leave off the -Credential parameter. The -Credential parameter will not work with Microsoft Accounts.

Select your Azure Subscription Context

After logging into Microsoft Azure, your user account might have been granted access to multiple Azure subscriptions. You need to make sure you’re operating on the correct subscription. First, list out your active Azure subscriptions using this command:

Get-AzureRmSubscription | Where-Object -FilterScript { $PSItem.State -eq 'Enabled' } | Format-Table -Property SubscriptionId, SubscriptionName -AutoSize

Choose a subscription from the list, and run the following command, to change subscription contexts:

Select-AzureRmSubscription -SubscriptionId <YourSubscriptionId>

After changing subscription contexts, you’re finally ready to find your existing network interface!

Find your Network Interface

Now that our environment is fully set up, the first thing you need to do is grab your existing network interface, that you want to update from dynamic to static (or vice versa). The following command will provide a nicely formatted table of Network Interface resources that have been provisioned in your Azure subscription (account).

NOTE: If you run Get-AzureRmNetworkInterface without adding some formatting, then you’ll get a ton of extra output that isn’t immediately relevant. Feel free to try it out yourself, but be prepared to scroll a lot!

Get-AzureRmNetworkInterface | Format-Table -Property ResourceGroupName, Name, Location -AutoSize

Once you have visually identified the Network Interface that you want to update, assign it to a variable. Remember, in Azure Resource Manager (ARM), every Resource must belong to a Resource Group. You’ll need to specify the -ResourceGroupName and -Name parameters in order to retrieve a specific Network Interface.

$TrevorNIC = Get-AzureRmNetworkInterface -ResourceGroupName TrevorNIC -Name TrevorNIC-01

Now we’ve stored a reference, to the in-memory Network Interface object, in the variable named $TrevorNIC.

Change the NIC’s Private IP Allocation Method

Using the object reference we just created in the previous step, we now want to change the Private IP Address’ Allocation Method. You can inspect the $TrevorNIC object by simply typing the variable and hitting ENTER. You’ll notice a property on the Network Interface object called IpConfigurations, which is an array of child objects. As of right now, each Azure Network Interface only supports a single IpConfiguration.

On the IpConfiguration child object itself, you’ll notice a property called PrivateIpAllocationMethod, which is probably currently set to Dynamic. We need to update this property to be Static instead. To do this, we’ll grab the IpConfigurations property, index into the first (and only) array element, and then set the allocation method property.

$TrevorNIC.IpConfigurations[0].PrivateIpAllocationMethod = 'Static'

There, that wasn’t so hard, was it? We’re not done yet though! We’ve only changed the object in-memory in our PowerShell session. We haven’t actually committed the change to Azure yet!

Committing the Network Interface Update

In the previous step, we changed the in-memory Network Interface object, but didn’t commit the changes to our Microsoft Azure subscription. This last step is actually the easiest part. Since we already have a reference to the in-memory object in the $TrevorNIC variable, we just call this command to update it:

Set-AzureRmNetworkInterface -NetworkInterface $TrevorNIC

After waiting a few moments, this command should return successfully, and your Network Interface will have been changed from Dynamic to Static! To validate that the change was successful, you can run Get-AzureRmNetworkInterface again, or visit the Microsoft Azure Portal.

Check out the documentation on Microsoft Azure Resource Manager (ARM) for more information about the management API itself. Thanks for reading!