Specifying custom virtual hard disk locations for Azure virtual machines

Specify virtual hard disk names programmatically
Specify virtual hard disk names programmatically

You can specify data disk location names while creating virtual machines using Azure PowerShell. This helps creating a consistent nomenclature which is helpful for your organization.

The New-AzureVMConfig cmdlet supports passing the uniform resource identifier (URI) to the location in storage that the operating system disk will be created in using the MediaLocation parameter. To override the creation location of data disks, you can specify the URI to the location in storage with the MediaLocation property of the Add-AzureDataDisk cmdlet.

To start with you first need to know the endpoints for your storage account. Let’s say I have a storage account named qastoragenortheurope, then I can get endpoints using:

(Get-AzureStorageAccount -StorageAccountName qastoragenortheurope).endpoints

Since virtual hard disks are stored in page blobs, the endpoint of our interest would be something like this:


I can then specify URIs for disk locations using below commands ( I would like my disks to be named after my virtual machine name to easily identify them):

$vmName = "metavrse-app-01"
$storage = "qastoragenortheurope"
$data1 = "https://$storage.blob.core.windows.net/disks/$vmName-data1.vhd"
$data2 = "https://$storage.blob.core.windows.net/disks/$vmName-data2.vhd"

Finally, we can leverage Add-AzureDataDisk cmdlet to help create data disks:

Add-AzureDataDisk -CreateNew -DiskSizeInGB 10 -LUN 0 -DiskLabel "data 1" -MediaLocation $data1

Putting it all together:

$adminUser = "itadmin"
$password = "itadmin@123"
$serviceName = "metavrseapp"
$location = "north europe"
$Size = (Get-AzureRoleSize | Where { $_.InstanceSize –eq "Standard_D1" }).InstanceSize
$vmName = "metavrse-app-01"
$imageFamily = "Windows Server 2012 R2 Datacenter"

$storage = "qastoragenortheurope"
$data1 = "https://$storage.blob.core.windows.net/disks/$vmName-data1.vhd"
$data2 = "https://$storage.blob.core.windows.net/disks/$vmName-data2.vhd"

$imageName = Get-AzureVMImage | 
 where { $_.ImageFamily -eq $imageFamily } |
 sort PublishedDate -Descending |
 select -ExpandProperty ImageName -First 1

New-AzureVMConfig -Name $vmName `
 -InstanceSize $size `
 -ImageName $imageName |
Add-AzureProvisioningConfig -Windows `
 -AdminUsername $adminUser `
 -Password $password |
Add-AzureDataDisk -CreateNew `
 -DiskSizeInGB 10 `
 -LUN 0 `
 -DiskLabel "data 1" `
 -MediaLocation $data1 |
Add-AzureDataDisk -CreateNew `
 -DiskSizeInGB 10 `
 -LUN 1 `
 -DiskLabel "Data 2" `
 -MediaLocation $data2 | 
Add-AzureEndpoint -Name "SQL" `
 -Protocol tcp `
 -LocalPort 1433 `
 -PublicPort 1433 |
New-AzureVM -ServiceName $serviceName `
 -Location $location

Do note that there is no benefit of specifying operating system disk name, as it would be named by Azure itself.

Also note that each successive data disk need to be added using an increment numerical value for LUN.  This is because each LUN can contain only one data disk.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s