Search code examples
azurepowershellnicazure-rm

Trying to add a second IP adress to a network adapter but getting Resource {0} not found on Set-AzureRmNetworkInterface


I am trying to add a second IP address on a network adapter on a Azure Virtual machine scale set using the AzureRm cmdlet using Powershell.

My code looks like this:

Add-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId "XXXXXXXXXXXXXXXXXXXXXX"
$vnetname = "confvnet"
$loc = "West Europe"
$backendSubnetName = "conf-jira-interlink"
$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -AddressPrefix "10.0.4.0/24"
echo "backendSubnetConfig: "$backendSubnetConfig

$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName "resourcegroup-confluence-jira-datacenter"
echo "vnet: "$vnet

Add-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet -AddressPrefix "10.0.4.0/24" | Set-AzureRmVirtualNetwork
$subnetId = (Get-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet).Id
echo "subnetId: "$subnetId

$ipCfg = New-AzureRmVmssIPConfig -Name 'eth1' -SubnetId $subnetId -PrivateIPAddressVersion 'IPv4' -Primary $false
echo "ipCfg: "$ipCfg

$backendSubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet
echo "backendSubnet: "$backendSubnet

$vmss = Get-AzureRmVmss -ResourceGroupName resourcegroup-confluence-jira-datacenter -VMScaleSetName confcluster
echo "vmss: "$vmss

Add-AzureRmVmssNetworkInterfaceConfiguration -Name $backendSubnet -Primary $false -IPConfiguration $ipCfg -VirtualMachineScaleSet $vmss
$vmss = Get-AzureRmVmss -ResourceGroupName resourcegroup-confluence-jira-datacenter -VMScaleSetName confcluster
$Nic = Get-AzureRmNetworkInterface -ResourceGroupName "resourcegroup-confluence-jira-datacenter" -VirtualMachineScaleSetName 'confcluster'
echo "Nic: "$Nic

$ipconfig_interlink = New-AzureRmNetworkInterfaceIpConfig -Name "ipconfig-conf-jira-interlink" -Subnet $backendSubnetConfig -PrivateIpAddress "10.0.4.20"
Add-AzureRmNetworkInterfaceIpConfig -Name $ipconfig_interlink.Name -Subnet $backendSubnetConfig -NetworkInterface $Nic
Set-AzureRmNetworkInterfaceIpConfig -Name $ipconfig_interlink.Name -PrivateIpAddress 10.0.4.20 -Subnet $backendSubnetConfig -NetworkInterface $Nic
$Nic = Get-AzureRmNetworkInterface -ResourceGroupName "resourcegroup-confluence-jira-datacenter" -VirtualMachineScaleSetName 'confcluster'
echo "Nic: "$Nic

Set-AzureRmNetworkInterface -NetworkInterface $Nic
Update-AzureRmVmss -ResourceGroupName "resourcegroup-confluence-jira-datacenter" -VMScaleSetName "confcluster" -VirtualMachineScaleSet $vmss

But I'm getting the error

Set-AzureRmNetworkInterface : Resource '{0}' not found In X:\JIRA_Confluence_Migration\PowerShell\network-interfaces-azure-same-vnet.ps1:67 Zeichen:1 + Set-AzureRmNetworkInterface -NetworkInterface $Nic + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Set-AzureRmNetworkInterface], ArgumentException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.SetAzureNetworkInterfaceCommand on Set-AzureRmNetworkInterface.

Does anyone know what the problem is?


Solution

  • For your issue, you just need to change the command like below:

    $Nic | Set-AzureRmNetworkInterface
    

    Then it will work without the error.

    But as I see in your script, you just get the network interface then set it without any change. If so, the command does not affect anything. You can take a look at the example of the command Set-AzureRmNetworkInterface.

    Update

    According to another test, there is a point should be focused on. Azure VM Scale Set instances are not the same as the VM. You can not find the network interfaces of the instances in the group which the VMSS in. All the instances are created from the same configuration, so do the network interfaces. As the error shows, the resource does not found.

    You can take a look at the question that How do I specify a range of private IP addresses to use for static private IP address allocation. So I think that it's a wrong way that you want to set the NIC of the instance through the command Set-AzureRmNetworkInterface.

    Just as your previous question, you can set the network configuration of the VMSS, it's the right way for VMSS.