Nicko
Nicko

Reputation: 195

How to assign a network interface to a newly created VM with ARM Powershell?

I'm getting started with ARM in our Azure tenancy...

Firstly, I created a resource group, TEST-RG.

I've created a Network Interface in my resource group using the Portal. Let's call it TEST-NIC in resource group TEST-RG.

I've also created a DS2_V2 VM called TEST-VM, running Windows 2012 R2 and also in TEST-RG.

How do I associate TEST-NIC with TEST-VM? I can't see a way of doing that in the Portal, nor can I see a way in the Portal of creating a VM with no network interface, such that I can add TEST-NIC later.

So, I assume this can only be done in PS, but I'm not at all clear how...

Have I lost the plot? Any guidance would be appreciated.

Thanks

Upvotes: 1

Views: 1316

Answers (2)

Jason Ye
Jason Ye

Reputation: 13954

For now, Azure does not support add a NIC to an existing VM.

can I see a way in the Portal of creating a VM with no network interface, such that I can add TEST-NIC later

We can't create a VM without NIC, but we can use PowerShell to create a new VM with this NIC.

Here is my script to create a new VM with existing NIC.

$ResourceGroupName = "nic"
$Location = "Eastus"
$StorageName = "jasondisk321"
$StorageType = "Standard_LRS"
$VMName = "myvm"
$VMSize = "Standard_DS2_v2"
$OSDiskName = $VMName + "OSDisk"
$StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -Type $StorageType -Location $Location
$nic = Get-AzureRmNetworkInterface -Name jasonnic -ResourceGroupName $rgname
$nicId = $nic.Id 
$Credential = Get-Credential
$vm = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$vm = Set-AzureRmVMOperatingSystem -VM $vm -ComputerName $VMName -Windows -Credential $Credential
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $vm

Upvotes: 2

Michael B
Michael B

Reputation: 12228

Firstly, you can't deploy an ARM VM without a NIC.

If you look at this script

It has the lines

$Interface = New-AzureRmNetworkInterface -Name $InterfaceName `
            -ResourceGroupName $ResourceGroupName `
            -Location $Location `
            -SubnetId $VNet.Subnets[0].Id `
            -PublicIpAddressId $PIp.Id

Which is later used by

$VirtualMachine = Add-AzureRmVMNetworkInterface `
            -VM $VirtualMachine `
            -Id $Interface.Id

So if you want to use an existing NIC you just need to pass the $interface.id (from Get-AzureRmVMNetworkInterface)

When you wrap those two around the rest of a VM deployment script you'll build a VM with a NIC

Upvotes: 1

Related Questions