更改 VM 的可用性集Change the availability set for a VM

以下步骤说明如何使用 Azure PowerShell 来更改 VM 的可用性集。The following steps describe how to change the availability set of a VM using Azure PowerShell. 只能在创建 VM 时将 VM 添加到可用性集。A VM can only be added to an availability set when it is created. 若要更改可用性集,必须将虚拟机删除,然后重新创建虚拟机。To change the availability set, you need to delete and then recreate the virtual machine.

本文同时适用于 Linux VM 和 Windows VM。This article applies to both Linux and Windows VMs.

本文最后一次使用 Az PowerShell 模块版本 1.2.0 在 2019 年 2 月 12 日进行了测试。This article was last tested on 2/12/2019 using the Az PowerShell module version 1.2.0.

本示例不检查 VM 是否已连接到负载均衡器。This example does not check to see if the VM is attached to a load balancer. 如果 VM 已连接到负载均衡器,则需要更新脚本以处理这种情况。If your VM is attached to a load balancer, you will need to update the script to handle that case.

更改可用性集Change the availability set

以下脚本提供一个示例,该示例收集所需的信息、删除原始 VM,并在新可用性集中重新创建 VM。The following script provides an example of gathering the required information, deleting the original VM and then recreating it in a new availability set.

# Set variables
    $resourceGroup = "myResourceGroup"
    $vmName = "myVM"
    $newAvailSetName = "myAvailabilitySet"

# Get the details of the VM to be moved to the Availability Set
    $originalVM = Get-AzVM `
       -ResourceGroupName $resourceGroup `
       -Name $vmName

# Create new availability set if it does not exist
    $availSet = Get-AzAvailabilitySet `
       -ResourceGroupName $resourceGroup `
       -Name $newAvailSetName `
       -ErrorAction Ignore
    if (-Not $availSet) {
    $availSet = New-AzAvailabilitySet `
       -Location $originalVM.Location `
       -Name $newAvailSetName `
       -ResourceGroupName $resourceGroup `
       -PlatformFaultDomainCount 2 `
       -PlatformUpdateDomainCount 2 `
       -Sku Aligned
    }

# Remove the original VM
    Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName    

# Create the basic configuration for the replacement VM. 
    $newVM = New-AzVMConfig `
       -VMName $originalVM.Name `
       -VMSize $originalVM.HardwareProfile.VmSize `
       -AvailabilitySetId $availSet.Id

# For a Linux VM, change the last parameter from -Windows to -Linux 
    Set-AzVMOSDisk `
       -VM $newVM -CreateOption Attach `
       -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
       -Name $originalVM.StorageProfile.OsDisk.Name `
       -Windows

# Add Data Disks
    foreach ($disk in $originalVM.StorageProfile.DataDisks) { 
    Add-AzVMDataDisk -VM $newVM `
       -Name $disk.Name `
       -ManagedDiskId $disk.ManagedDisk.Id `
       -Caching $disk.Caching `
       -Lun $disk.Lun `
       -DiskSizeInGB $disk.DiskSizeGB `
       -CreateOption Attach
    }

# Add NIC(s) and keep the same NIC as primary
    foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {    
    if ($nic.Primary -eq "True")
        {
            Add-AzVMNetworkInterface `
            -VM $newVM `
            -Id $nic.Id -Primary
            }
        else
            {
              Add-AzVMNetworkInterface `
              -VM $newVM `
              -Id $nic.Id 
                }
    }

# Recreate the VM
    New-AzVM `
       -ResourceGroupName $resourceGroup `
       -Location $originalVM.Location `
       -VM $newVM `
       -DisableBginfoExtension

后续步骤Next steps

通过添加附加数据磁盘,向 VM 添加附加存储。Add additional storage to your VM by adding an additional data disk.