Hello,
I have a basic script where I can create one disk on a VM. I’d like to be able to create multiple disk with different sizes and names. I’m unsure how to get started on this.
This is what I have so far:
[pre]
connect to Azure
Connect-AzAccount
Set-AzContext -Subscription “<sub_name>”
$rgName = “<rg_name>”
$vmName = “azr-tst-01”
$diskName = $vmName.Substring(0,12) + “_Data”
create the initial configuration
$diskConfig = New-AzDiskConfig `
-Location “EastUS2” `
-CreateOption Empty `
-DiskSizeGB 100
create the data disk
$dataDisk = New-AzDisk `
-ResourceGroupName $rgName `
-DiskName $diskName `
-Disk $diskConfig
get the VM where you want to add the data disk
$vm = Get-AzVM -ResourceGroupName $rgName `
-Name $vmName
add the data disk to the VM configuration
$vm = Add-AzVMDataDisk `
-VM $vm `
-Name $diskName `
-CreateOption Attach `
-ManagedDiskId $dataDisk.Id `
-Lun 1
update the VM
Update-AzVM -ResourceGroupName $rgName -VM $vm
prepare the data disks
#Get-AzDisk -DiskName $diskName | Where partitionstyle -EQ ‘raw’
[/pre]
Thanks,
Frank
You repeat lines 9-36 for each disk
A for loop as such (just an example) before 9 and after 36:
$disks = 4
$vmName = “azr-tst-01”
$diskName = $vmName.Substring(0,12) + “_Data”
for ($i=1; $i -le $disks; $i++) {
Write-Host $diskName $i
}
But how do I have a unique name for each disk? I’d like to add a special name like data, log, etc for each disk, not just a number.
Then you could create a loop for each of those names, or in the loop, have disk 1 be Data, 2 be logs and so on.
Example: IF ($I -eq 1) {$Diskname = Data}
Thanks for that!
i.e.)
[pre]
$disks = 4
$vmName = “azr-tst-01”
$diskName = $vmName.Substring(0,12) + ‘_00’
for ($i=1; $i -le $disks; $i++) {
if ($i -eq 1) {
Write-Host ($diskName + $i)
}
elseif ($i -eq 2) {
Write-Host ($diskname + $i)
}
else {
Write-Host “No more disks left”
}
}
[/pre]
How would you incorporate varying disk sizes?
This would be the loop, but I’m unsure how I incorporate the if statement:?
[pre]
for ($i=1; $i -le $numDisks; $i++) {
create the initial configuration
$diskConfig = New-AzDiskConfig `
-Location “EastUS2” `
-CreateOption Empty `
-DiskSizeGB 100
create the data disk
$dataDisk = New-AzDisk `
-ResourceGroupName $rgName `
-DiskName $diskName `
-Disk $diskConfig
get the VM where you want to add the data disk
$vm = Get-AzVM -ResourceGroupName $rgName `
-Name $vmName
add the data disk to the VM configuration
$vm = Add-AzVMDataDisk `
-VM $vm `
-Name $diskName `
-CreateOption Attach `
-ManagedDiskId $dataDisk.Id `
-Lun 1
}
[/pre]
It would probably be easier to create a csv file, and pull that as variables into your script.
$disks=(import-csv Disks.csv)
foreach ($disk in $disks){
your script logic
}