Edit

Share via


Add-AzVMDataDisk

Adds a data disk to a virtual machine.

Syntax

VmNormalDiskParameterSetName (Default)

Add-AzVMDataDisk
    [-VM] <PSVirtualMachine>
    [[-Name] <String>]
    [[-VhdUri] <String>]
    [[-Caching] <CachingTypes>]
    [[-DiskSizeInGB] <Int32>]
    [-Lun] <Int32>
    [-CreateOption] <String>
    [[-SourceImageUri] <String>]
    [-DiskEncryptionSetId <String>]
    [-DeleteOption <String>]
    [-SourceResourceId <String>]
    [-DefaultProfile <IAzureContextContainer>]
    [<CommonParameters>]

VmManagedDiskParameterSetName

Add-AzVMDataDisk
    [-VM] <PSVirtualMachine>
    [[-Name] <String>]
    [[-Caching] <CachingTypes>]
    [[-DiskSizeInGB] <Int32>]
    [-Lun] <Int32>
    [-CreateOption] <String>
    [[-ManagedDiskId] <String>]
    [[-StorageAccountType] <String>]
    [-DiskEncryptionSetId <String>]
    [-WriteAccelerator]
    [-DeleteOption <String>]
    [-SourceResourceId <String>]
    [-DefaultProfile <IAzureContextContainer>]
    [<CommonParameters>]

Description

The Add-AzVMDataDisk cmdlet adds a data disk to a virtual machine. You can add a data disk when you create a virtual machine, or you can add a data disk to an existing virtual machine.

Examples

Example 1: Add data disks to a new virtual machine

$VirtualMachine = New-AzVMConfig -VMName "VirtualMachine07" -VMSize "Standard_A1"
$DataDiskVhdUri01 = "https://contoso.blob.core.windows.net/test/data1.vhd"
$DataDiskVhdUri02 = "https://contoso.blob.core.windows.net/test/data2.vhd"
$DataDiskVhdUri03 = "https://contoso.blob.core.windows.net/test/data3.vhd"
$VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -Name 'DataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 0 -VhdUri $DataDiskVhdUri01 -CreateOption Empty
$VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -Name 'DataDisk2' -Caching 'ReadOnly' -DiskSizeInGB 11 -Lun 1 -VhdUri $DataDiskVhdUri02 -CreateOption Empty
$VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -Name 'DataDisk3' -Caching 'ReadOnly' -DiskSizeInGB 12 -Lun 2 -VhdUri $DataDiskVhdUri03 -CreateOption Empty

The first command creates a virtual machine object, and then stores it in the $VirtualMachine variable. The command assigns a name and size to the virtual machine. The next three commands assign paths of three data disks to the $DataDiskVhdUri01, $DataDiskVhdUri02, and $DataDiskVhdUri03 variables. This approach is only for readability of the following commands. The final three commands each adds a data disk to the virtual machine stored in $VirtualMachine. The command specifies the name and location for the disk, and other properties of the disk. The URI of each disk is stored in $DataDiskVhdUri01, $DataDiskVhdUri02, and $DataDiskVhdUri03.

Example 2: Add a data disk to an existing virtual machine

$VirtualMachine = Get-AzVM -ResourceGroupName "ResourceGroup11" -Name "VirtualMachine07"
Add-AzVMDataDisk -VM $VirtualMachine -Name "disk1" -VhdUri "https://contoso.blob.core.windows.net/vhds/diskstandard03.vhd" -LUN 0 -Caching ReadOnly -DiskSizeinGB 1 -CreateOption Empty
Update-AzVM -ResourceGroupName "ResourceGroup11" -VM $VirtualMachine

The first command gets the virtual machine named VirtualMachine07 by using the Get-AzVM cmdlet. The command stores the virtual machine in the $VirtualMachine variable. The second command adds a data disk to the virtual machine stored in $VirtualMachine. The final command updates the state of the virtual machine stored in $VirtualMachine in ResourceGroup11.

Example 3: Add a data disk to a new virtual machine from a generalized user image

$VirtualMachine = New-AzVMConfig -VMName "VirtualMachine07" -VMSize "Standard_A1"
$DataImageUri = "https://contoso.blob.core.windows.net/system/Microsoft.Compute/Images/captured/dataimage.vhd"
$DataDiskUri = "https://contoso.blob.core.windows.net/test/datadisk.vhd"
$VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -Name "disk1" -SourceImageUri $DataImageUri -VhdUri $DataDiskUri -Lun 0 -DiskSizeinGB 10 -CreateOption FromImage

The first command creates a virtual machine object and stores it in the $VirtualMachine variable. The command assigns a name and size to the virtual machine. The next two commands assign paths for the data image and data disks to the $DataImageUri and $DataDiskUri variables respectively. This approach is used to improve the readability of the following commands. The final commands adds a data disk to the virtual machine stored in $VirtualMachine. The command specifies the name and location for the disk and other properties of the disk.

Example 4: Add data disks to a new virtual machine from a specialized user image

$VirtualMachine = New-AzVMConfig -VMName "VirtualMachine07" -VMSize "Standard_A1"
$DataDiskUri = "https://contoso.blob.core.windows.net/test/datadisk.vhd"
$VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -Name "dd1" -VhdUri $DataDiskUri -Lun 0 -DiskSizeinGB 10 -CreateOption Attach

The first command creates a virtual machine object and stores it in the $VirtualMachine variable. The command assigns a name and size to the virtual machine. The next commands assigns paths of the data disk to the $DataDiskUri variable. This approach is used to improve the readability of the following commands. The final command add a data disk to the virtual machine stored in $VirtualMachine. The command specifies the name and location for the disk, and other properties of the disk.

Parameters

-Caching

Specifies the caching mode of the disk. The acceptable values for this parameter are:

  • ReadOnly
  • ReadWrite
  • None The default value is ReadWrite. Changing this value causes the virtual machine to restart. This setting affects the consistency and performance of the disk.

Parameter properties

Type:CachingTypes
Default value:None
Accepted values:None, ReadOnly, ReadWrite
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:3
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-CreateOption

Specifies whether this cmdlet creates a disk in the virtual machine from a platform or user image, creates an empty disk, or attaches an existing disk. The acceptable values for this parameter are:

  • Attach. Specify this option to create a virtual machine from a specialized disk. When you specify this option, do not specify the SourceImageUri parameter. The VhdUri is all that is needed in order to tell the Azure platform the location of the virtual hard disk (VHD) to attach as a data disk to the virtual machine.
  • Empty. Specify this to create an empty data disk.
  • FromImage. Specify this option to create a virtual machine from a generalized image or disk. When you specify this option, you must specify the SourceImageUri parameter also in order to tell the Azure platform the location of the VHD to attach as a data disk. The VhdUri parameter is used as the location identifying where the data disk VHD will be stored when it is used by the virtual machine.
  • Empty. This value is used when creating an empty data disk.
  • Copy. This value is used to create a data disk from a snapshot or another disk. Restore: This value is used to create a data disk from a disk restore point.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:6
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-DefaultProfile

The credentials, account, tenant, and subscription used for communication with azure.

Parameter properties

Type:IAzureContextContainer
Default value:None
Supports wildcards:False
DontShow:False
Aliases:AzContext, AzureRmContext, AzureCredential

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-DeleteOption

Data Disk Delete Option. Specifies what action to perform on the disk after VM deletion. Options are: Detach, Delete.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-DiskEncryptionSetId

Specifies the resource Id of customer managed disk encryption set. This can only be specified for managed disk.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-DiskSizeInGB

Specifies the size, in gigabytes, of an empty disk to attach to a virtual machine.

Parameter properties

Type:

Nullable<T>[Int32]

Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:4
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-Lun

Specifies the logical unit number (LUN) for a data disk.

Parameter properties

Type:

Nullable<T>[Int32]

Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:5
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-ManagedDiskId

Specifies the ID of a managed disk.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

VmManagedDiskParameterSetName
Position:8
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-Name

Specifies the name of the data disk to add.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:1
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-SourceImageUri

Specifies the source URI of the disk that this cmdlet attaches.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False
Aliases:SourceImage

Parameter sets

VmNormalDiskParameterSetName
Position:7
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-SourceResourceId

ARM ID of snapshot or disk restore point from which to create a disk.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-StorageAccountType

Specifies the storage account type of managed disk.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

VmManagedDiskParameterSetName
Position:9
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-VhdUri

Specifies the Uniform Resource Identifier (URI) for the virtual hard disk (VHD) file to create when a platform image or user image is used. This cmdlet copies the image binary large object (blob) to this location. This is the location from which to start the virtual machine.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

VmNormalDiskParameterSetName
Position:2
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:True
Value from remaining arguments:False

-VM

Specifies the local virtual machine object to which to add a data disk. You can use the Get-AzVM cmdlet to obtain a virtual machine object. You can use the New-AzVMConfig cmdlet to create a virtual machine object.

Parameter properties

Type:PSVirtualMachine
Default value:None
Supports wildcards:False
DontShow:False
Aliases:VMProfile

Parameter sets

(All)
Position:0
Mandatory:True
Value from pipeline:True
Value from pipeline by property name:True
Value from remaining arguments:False

-WriteAccelerator

Specifies whether WriteAccelerator should be enabled or disabled on a managed data disk.

Parameter properties

Type:SwitchParameter
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

VmManagedDiskParameterSetName
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs

PSVirtualMachine

String

CachingTypes

Nullable<T>

Outputs

PSVirtualMachine

PSVirtualMachineScaleSetVM