如何使用 ARM Powershell 将网络接口分配给新创建的 VM?

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

我开始在我们的 Azure 租赁中使用 ARM...

首先,我创建了一个资源组,TEST-RG

我已经使用门户在我的资源组中创建了一个网络接口。我们在资源组 TEST-RG.

中称它为 TEST-NIC

我还创建了一个 DS2_V2 虚拟机,名为 TEST-VM、运行 Windows 2012 R2 以及 TEST-RG

如何将 TEST-NICTEST-VM 相关联?我在 Portal 中看不到这样做的方法,也看不到在 Portal 中创建没有网络接口的 VM 的方法,这样我可以稍后添加 TEST-NIC

所以,我认为这只能在 PS 中完成,但我完全不清楚如何...

我是不是丢剧情了?任何指导将不胜感激。

谢谢

首先,您无法在没有 NIC 的情况下部署 ARM VM。

如果你查看 this 脚本

它有行

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

后来被

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

因此,如果您想使用现有的 NIC,您只需传递 $interface.id(来自 Get-AzureRmVMNetworkInterface)

当您将这两个包裹在 VM 部署脚本的其余部分时,您将构建一个带有 NIC 的 VM

目前,Azure 支持将 NIC 添加到现有 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

我们无法创建没有 NIC 的 VM,但我们可以使用 PowerShell 使用此 NIC 创建新的 VM。

这是我使用现有 NIC.

创建新 VM 的脚本
$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