您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 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:

本文在 Azure Cloud Shell中使用 PowerShell,它不断更新到最新版本。This article uses PowerShell within the Azure Cloud Shell, which is constantly updated to the latest version. 若要打开 Cloud Shell,请从任何代码块的顶部选择“试一试”。To open the Cloud Shell, select Try it from the top of any code block.

将空数据磁盘添加到虚拟机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 = 'East US' 
$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

在可用性区域中使用托管磁盘Using managed disks in an Availability Zone

若要在可用性区域中创建磁盘,请将 New-AzDiskConfig-Zone 参数一起使用。To create a disk in an Availability Zone, use New-AzDiskConfig with the -Zone parameter. 以下示例在区域 1 中创建一个磁盘。The following example creates a disk in zone 1.

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

$diskConfig = New-AzDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128 -Zone 1
$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

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

$rgName = "myResourceGroup"
$vmName = "myVM"
$location = "East US" 
$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.