0
votes

I am writing a script to clone a VM(unmanaged to unmanaged disk) in Azure, i want to know how to create NEW NIC using existing VM NIC config(same VNet,Subnet) in the same RG.

1
Well, Do you just want to New NIC have same Subnet in the same RG?Wayne Yang
yes but i want the subnet and RG to be fetched from source VM NIC subnet and RGharish kumar
You can check my answer and be free to update here.Wayne Yang
am unable to store values in $subnet = $oldnic.nic.IpConfigurations.subnet attaching my error details. i.stack.imgur.com/wRk5g.pngharish kumar

1 Answers

0
votes

If I understand correctly, you want to create w new NIC with the same SubNet and VNet as the an old NIc in the same Resource Group.

You can use the following powershell scripts :

$destinationResourceGroup = <yourRGName>
$location = <thedestinationlocation>
$oldnic = Get-AzureRmNetworkInterface -ResourceGroupName $destinationResourceGroup -Name <theOldNICName>
$subnet = $oldnic.nic.IpConfigurations.subnet
$newnic = New-AzureRmNetworkInterface -Name <newnicName> -ResourceGroupName $destinationResourceGroup -Location $location  -SubnetId $subnet.Id 

If you can also associate other resources to the new NIC (One IPaddress can only associate to one NIC)

$newnic = New-AzureRmNetworkInterface -Name <newnicName> -ResourceGroupName $destinationResourceGroup -Location $location -SubnetId $subnet.Id    -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

Additonal: You may want to use -IpConfiguration $oldnic.IpConfigurations to copy the same Ipconfiguration, but you would came across one issue about the associated IP address.

So, If you just want to copy a NIC, you'd better not use this command. But if you can deallocate the IPaddress after getting the Ipconfigurations of the old NIC, you can use it.

Hope this helps!