Tuesday 5 September 2017

Modifying the primary Network Interface on an Azure IaaS VM using Power Shell

When you have a multi-NIC Azure IaaS VM, one of the NIC's must be marked as the "primary" NIC. This can only be configured using Power Shell.

The Azure IaaS VM instance must be in the stopped state before the NIC's can be modified

Stop-AzureRmVM -Name "myNVA" -ResourceGroupName "myNVA"

Set a variable for the Azure IaaS VM you want to interrogate

$vm = Get-AzureRmVm -Name "myNVA" -ResourceGroupName "myNVA"

Display all the network interfaces attached to that Azure IaaS VM

$vm.NetworkProfile.NetworkInterfaces


Set the priority of the Network Interface using the following commands, the primary should be set to “True”

$vm.NetworkProfile.NetworkInterfaces[0].Primary = $false
$vm.NetworkProfile.NetworkInterfaces[1].Primary = $true

Update-AzureRmVM -VM $vm -ResourceGroupName "myNVA"

Instructs Azure to start up the VM
Start-AzureRmVM -ResourceGroupName "myNVA" -Name "myNVA"


Update

The code must be in this order

Stop-AzureRmVM -Name "GBAS1SER04" -ResourceGroupName "GBAS1-RG-FS-01"

$vm = Get-AzureRmVm -Name "GBAS1SER04" -ResourceGroupName "GBAS1-RG-FS-01"
$nicId = (Get-AzureRmNetworkInterface -ResourceGroupName "GBAS1-RG-FS-01" -Name "GBAS1SER04-NI-01").Id

Add-AzureRmVMNetworkInterface -VM $vm -Id $nicId

$vm.NetworkProfile.NetworkInterfaces[0].Primary = $false
$vm.NetworkProfile.NetworkInterfaces[1].Primary = $true

Update-AzureRmVM -ResourceGroupName "GBAS1-RG-FS-01" -VM $vm

If you do not put the

$vm.NetworkProfile.NetworkInterfaces[0].Primary = $false
$vm.NetworkProfile.NetworkInterfaces[1].Primary = $true

Section before the Update-AzureRmVM the script will fail.