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

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

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

在开始之前,请查看以下提示:Before you do this, review these tips:

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可以通过两种方式来启动 Cloud Shell:There are two ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud ShellCloud Shell in the portal

如果选择在本地安装并使用 PowerShell,则本教程需要 Azure PowerShell 模块版本 3.6 或更高版本。If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 3.6 or later. 运行 Get-Module -ListAvailable AzureRM 即可查找版本。Run Get-Module -ListAvailable AzureRM to find the version. 如果需要升级,请参阅安装 Azure PowerShell 模块If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzureRmAccount 以创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzureRmAccount to create a connection with Azure.

将空数据磁盘添加到虚拟机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 = 'PremiumLRS'
$dataDiskName = $vmName + '_datadisk1'

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

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

Update-AzureRmVM -VM $vm -ResourceGroupName $rgName

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

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

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

$diskConfig = New-AzureRmDiskConfig -SkuName $storageType -Location $location -CreateOption Empty -DiskSizeGB 128 -Zone 1
$dataDisk1 = New-AzureRmDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $rgName

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

Update-AzureRmVM -VM $vm -ResourceGroupName $rgName

初始化磁盘Initialize the disk

添加空磁盘后,需对其进行初始化。After you add an empty disk, you need to initialize it. 要初始化该磁盘,可以登录到一个 VM,并使用磁盘管理进行初始化。To initialize the disk, you can log 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-AzureRmVMCustomScriptExtension -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 something like this code to initialize the disks:

    $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-AzureRmDisk -ResourceGroupName $rgName -DiskName $dataDiskName 

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

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

Update-AzureRmVM -VM $vm -ResourceGroupName $rgName

后续步骤Next steps

创建快照Create a snapshot.