Martin Erlic
Martin Erlic

Reputation: 5665

Adding a disk to an Azure VM using PowerShell ARM script

I'm trying to add a disk to a VM in Azure using the following PowerShell script:

$rgName = "${resourceManager}"
$stName = "${storageAccount}"
$diskname="${diskName}"
$lun = ${lunNum}
$vmName = "${vmName}"
$diskSize=${diskSize}

$storageAcc=Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $stName
$diskpath=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" +  $diskname  + ".vhd"
$vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName | Add-AzureRmVMDataDisk -Name $diskname -VhdUri $diskpath -CreateOption empty -DiskSizeInGB $diskSize -Lun $lun
$vm.Tags = $null
$vm | Update-AzureRmVM

The following are descriptions of the values I need:

SubscriptionID - Azure Subscription ID
AzureUsername - Name of Azure service account. It must be configured as co-administrator on the Azure Subscription
AzurePassword - Associated Azure service account password
resourceManager - Name of Resource Manager (RM) containing the VM
storageAccount - Name of storage account (within the specified RM), where to create the new disk
diskName - desired name of the disk
diskSize - desired size of the disk in Giga Bytes, this is a quota, up to 1000 GB is allowed
lunNum - the lun number to use, valid values are 0,1,2,3, etc.
vmName - the name of an existing VM where to attach the new disk

The following are test values I use in the script:

SubscriptionID - 06e2bb3d-e66d-4a21-a4a0-78b2fe53f8c6
AzureUsername – [email protected]    
AzurePassword – test123
resourceManager – 
storageAccount -
diskName - test
diskSize - 100
lunNum - 
vmName - ALPHABETTVM001

I'm unsure where to procure the resourceManager, storageAccount, and lunNum inputs. I'm functionally testing Azure scripts that have been converted from the old ASM to new ARM model, but I don't have a lot of experience with this. Where can I find these three values for my given virtual machine?

Upvotes: 2

Views: 745

Answers (1)

Jason Ye
Jason Ye

Reputation: 13954

In Azure resource module (ARM, new), we can use Login-AzureRmAccount to login Azure via PowerShell.

I'm unsure where to procure the resourceManager, storageAccount, and lunNum inputs.

You can login the Azure via PowerShell, and list all the resource group name:
$rgName = "resource group name"

Get-AzureRmResourceGroup | select resourcegroupname
ResourceGroupName
-----------------
Api-Default-North-Central-US
AppResource
CS-WebJobs-NorthCentralUS-scheduler
Default-ApplicationInsights-CentralUS
Default-Networking
Default-ServiceBus-CentralUS
Default-SQL-CentralUS
Default-Storage-EastUS
Default-Web-NorthCentralUS
DemoVS8172016
DennisSPMonitor
lb1
win

Also you can use new portal to find the resource group: enter image description here
About storage account, we can use PowerShell to list all the storage account in the resource group(also you can find the storage account name via the new portal):

Get-AzureRmStorageAccount -ResourceGroupName win  | select storageaccountname
StorageAccountName
------------------
windiag865  (used for log)
windisks548 (used for data)

About lunnum, if your VM just have one OS disk without datadisk, the first lunnum is 0 by default, after you add a data disk to the VM, you can use PowerShell to list the Lun number:

get-azurermvm -ResourceGroupName "win" -Name "win"
StorageProfile             :
  ImageReference           :
    Publisher              : MicrosoftWindowsServer
    Offer                  : WindowsServer
    Sku                    : 2012-R2-Datacenter
    Version                : latest
  OsDisk                   :
    OsType                 : Windows
    Name                   : win
    Vhd                    :
      Uri                  : https://windisks548.blob.core.windows.net/vhds/win20170111102348.vhd
    Caching                : ReadWrite
    CreateOption           : FromImage
  DataDisks[0]             :
    Lun                    : 0
    Name                   : win-20170111-103800
    Vhd                    :
      Uri                  : https://windisks548.blob.core.windows.net/vhds/win-20170111-103800.vhd
    Caching                : None
    CreateOption           : Empty
    DiskSizeGB             : 50
DataDiskNames[0]           : win-20170111-103800

PS > (get-azurermvm -ResourceGroupName "win" -Name "win").StorageProfile.DataDisks.lun
0

Upvotes: 1

Related Questions