Azure – How to change the subnet of a secondary NIC in Azure

azuresubnet

I have a VM with 3 NICs that was deployed from the Marketplace.
I need to change the subnets and IP addresses that are currently assigned to NICs 2 & 3
I have tried using PowerShell scripts but receive this error:
Set-AzureRmNetworkInterface : Cannot change subnet of network interface /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
4999/resourceGroups/vcfrg/providers/Microsoft.Network/networkInterfaces/vm200-eth1 while it is in use as a
secondary network interface.

Thanks,
Scott

Best Answer

I had create a VM with two NICs, NIC01(10.0.1.0/24) and NIC02(10.0.2.0/24), then I use powershell to set the azure network interface, here is my script, after the script complete, the NIC01 change to 10.0.3.0/24. It works for me.

$NICname = nic01
$RGname = jason
$NIC = Get-AzureRmNetworkInterface -Name $NICname -ResourceGroupName $RGname
$NIC.IpConfigurations[0].PrivateIpAddress 
$VNET = Get-AzureRmVirtualNetwork -Name $VNETname  -ResourceGroupName $RGname
$Subnet2 = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNET -Name subnet03
$NIC.IpConfigurations[0].Subnet.Id = $Subnet2.Id
Set-AzureRmNetworkInterface -NetworkInterface $NIC

enter image description here

And here is the result: enter image description here

Related Topic