使用 PowerShell 将数据磁盘附加到 Windows VMAttach a data disk to a Windows VM with PowerShell

本文介绍了如何使用 PowerShell 将新磁盘和现有磁盘附加到 Windows 虚拟机。This article shows you how to attach both new and existing disks to a Windows virtual machine by using PowerShell.

首先,查看以下提示:First, review these tips:

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

将空数据磁盘添加到虚拟机Add an empty data disk to a virtual machine

此示例演示了如何将空数据磁盘添加到现有的虚拟机。This example shows how to add an empty data disk to an existing virtual machine.

使用托管磁盘Using managed disks

$rgName = 'myResourceGroup'
$vmName = 'myVM'
$location = 'China East' 
$storageType = 'Premium_LRS'
$dataDiskName = $vmName + '_datadisk1'

$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128
$dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName

$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName 
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1

Update-AzVM -VM $vm -ResourceGroupName $rgName

初始化磁盘Initialize the disk

添加空磁盘后,需要对其进行初始化。After you add an empty disk, you'll need to initialize it. 要初始化该磁盘,可以登录到一个 VM,并使用磁盘管理进行初始化。To initialize the disk, you can sign in to a VM and use disk management. 如果在创建 VM 时在其上启用了 WinRM 和证书,则可以使用远程 PowerShell 初始化该磁盘。If you enabled WinRM and a certificate on the VM when you created it, you can use remote PowerShell to initialize the disk. 还可以使用自定义脚本扩展:You can also use a custom script extension:

$location = "location-name"
$scriptName = "script-name"
$fileName = "script-file-name"
Set-AzVMCustomScriptExtension -ResourceGroupName $rgName -Location $locName -VMName $vmName -Name $scriptName -TypeHandlerVersion "1.4" -StorageAccountName "mystore1" -StorageAccountKey "primary-key" -FileName $fileName -ContainerName "scripts"

脚本文件可以包含用来初始化磁盘的代码,例如:The script file can contain code to initialize the disks, for example:

$disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number

$letters = 70..89 | ForEach-Object { [char]$_ }
$count = 0
$labels = "data1","data2"

foreach ($disk in $disks) {
    $driveLetter = $letters[$count].ToString()
    $disk | 
    Initialize-Disk -PartitionStyle MBR -PassThru |
    New-Partition -UseMaximumSize -DriveLetter $driveLetter |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel $labels[$count] -Confirm:$false -Force
    $count++
}

将现有数据磁盘附加到 VMAttach an existing data disk to a VM

可以将现有托管磁盘作为数据磁盘附加到 VM。You can attach an existing managed disk to a VM as a data disk.

$rgName = "myResourceGroup"
$vmName = "myVM"
$location = "China East" 
$dataDiskName = "myDisk"
$disk = Get-AzDisk -ResourceGroupName $rgName -DiskName $dataDiskName 

$vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName 

$vm = Add-AzVMDataDisk -CreateOption Attach -Lun 0 -VM $vm -ManagedDiskId $disk.Id

Update-AzVM -VM $vm -ResourceGroupName $rgName

后续步骤Next steps

还可以使用模板部署托管磁盘。You can also deploy managed disks using templates. 有关详细信息,请参阅使用 Azure 资源管理器模板中的托管磁盘快速入门模板以部署多个数据磁盘。For more information, see Using Managed Disks in Azure Resource Manager Templates or the quickstart template for deploying multiple data disks.