1
votes

I have a VM NIC that has an IP(Primary) added to it. I am trying to find a way how I can add more IP's to the NIC.I am using a piece of code and it throws an error saying.

The final solution should actually give me to add 4 ip additonal to the NIC and update the first Ip which is primary and also update dns ip to the NIC.

Error Thrown: nic1-test-ipConfig cannot be deleted. Deletion and renaming of primary IP Configuration is not supported StatusCode: 400 ReasonPhrase: Bad Request ErrorCode: IpConfigDeleteNotSupported ErrorMessage: IP Configuration ProdNuma1-nic1-test-ipConfig cannot be deleted.

$vnet = Get-AzVirtualNetwork -Name "vnet" -ResourceGroupName "rg"
$subnet1 = Get-AzVirtualNetworkSubnetConfig -Name "sn" -VirtualNetwork $vnet
$location="westcentralus"
$vm="test"

#Adding IP's to ->NIC1
$ipc11 = New-AzNetworkInterfaceIpConfig -Name "ipconfig1" -PrivateIpAddress "10.64.13.10" -Primary -Subnet $subnet1
$ipc12 = New-AzNetworkInterfaceIpConfig -Name "ipconfig2" -PrivateIpAddress "10.64.13.11" -Subnet $subnet1
$ipc13 = New-AzNetworkInterfaceIpConfig -Name "ipconfig3" -PrivateIpAddress "10.64.13.12" -Subnet $subnet1
$ipc14 = New-AzNetworkInterfaceIpConfig -Name "ipconfig4" -PrivateIpAddress "10.64.13.13" -Subnet $subnet1
$ipc15 = New-AzNetworkInterfaceIpConfig -Name "ipconfig5" -PrivateIpAddress "10.64.13.14" -Subnet $subnet1


$NIC1 = New-AzNetworkInterface -Name "nic1-test" -ResourceGroupName "rg" -force -Location $location -IpConfiguration $ipc12,$ipc13,$ipc14,$ipc15 -DnsServer "10.64.2.00","10.64.0.01"

1

1 Answers

0
votes

Since the NIC already exists, you should be updating ipconfig1 with Set-AzNetworkInterfaceIpConfig, then adding the others with Add-AzNetworkInterfaceIpConfig. Then you can update the NIC with Set-AzNetworkInterface.

$vnet = Get-AzVirtualNetwork -Name "vnet" -ResourceGroupName "rg"
$subnet1 = Get-AzVirtualNetworkSubnetConfig -Name "sn" -VirtualNetwork $vnet

$vm = Get-AzVM -Name "vm" -ResourceGroupName "rg"

$nicName = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split("/")[-1]

$nic = Get-AzNetworkInterface -Name $nicName -ResourceGroupName "rg"

$nic | Set-AzNetworkInterfaceIpConfig -Name "ipconfig1" -PrivateIpAddress "10.64.13.10" -Primary -Subnet $subnet1
$nic | Add-AzNetworkInterfaceIpConfig -Name "ipconfig2" -PrivateIpAddress "10.64.13.11" -Subnet $subnet1
$nic | Add-AzNetworkInterfaceIpConfig -Name "ipconfig3" -PrivateIpAddress "10.64.13.12" -Subnet $subnet1
$nic | Add-AzNetworkInterfaceIpConfig -Name "ipconfig4" -PrivateIpAddress "10.64.13.13" -Subnet $subnet1
$nic | Add-AzNetworkInterfaceIpConfig -Name "ipconfig5" -PrivateIpAddress "10.64.13.14" -Subnet $subnet1

$nic | Set-AzNetworkInterface

If you have many NIC IP configurations to add, you could use an array of hashtables and make use of splatting. See about_Splatting for more information.

$secondaryIpConfigs = @(
    @{
        Name             = "ipconfig1"
        PrivateIpAddress = "10.64.13.10"
        Subnet           = $subnet1
        Primary          = $true
    },
    @{
        Name             = "ipconfig2"
        PrivateIpAddress = "10.64.13.11"
        Subnet           = $subnet1
    },
    @{
        Name             = "ipconfig3"
        PrivateIpAddress = "10.64.13.12"
        Subnet           = $subnet1
    },
    @{
        Name             = "ipconfig4"
        PrivateIpAddress = "10.64.13.13"
        Subnet           = $subnet1
    },
    @{
        Name             = "ipconfig5"
        PrivateIpAddress = "10.64.13.14"
        Subnet           = $subnet1
    }
)

foreach ($ipconfig in $secondaryIpConfigs) {
    $nic | Add-AzNetworkInterfaceIpConfig @ipconfig
}

$nic | Set-AzNetworkInterface

However, Add-AzNetworkInterfaceIpConfig will throw an error if the ipconfig already exists. You can use Get-AzNetworkInterfaceIpConfig to first check if the ipconfig exists, and add/update accordingly. To make sure the program doesn't throw an error immediately if an ipconfig is not found, we can use -ErrorAction SilentlyContinue.

foreach ($ipconfig in $secondaryIpConfigs) {

    if ($null -eq (Get-AzNetworkInterfaceIpConfig `
                -Name $ipconfig.Name `
                -NetworkInterface $nic `
                -ErrorAction SilentlyContinue)) {
            
        $nic | Add-AzNetworkInterfaceIpConfig @ipconfig
    }
    else {
        $nic | Set-AzNetworkInterfaceIpConfig @ipconfig
    }
}

$nic | Set-AzNetworkInterface