Script to Change Disk type inside Azure VM(HDD or SSD or Premium SSD)
---------------------------------------------------------------------------------------
Important:
Disclaimer:
The below script is tested but it might need to be customised in accordance with the environment/Requirement. Please test this in you LAB/POC environment before trying this in Production VM's.
----------------------------------------------------------------------------------------
Prerequisites:
Install Azure Az command in Powershell
https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-6.3.0
Connect Azure inside Powershell
Connect-AzAccount
Select subscription
Get-AzSubscription -SubscriptionName "Subscription Name" | select-AzSubscription
------------------------------------------------------------------------------------------
#Name of the Azure Resource Group on which the VM's are located
$rgName = 'Resource Group Name'
# Choose Disk Type Standard_LRS, StandardSSD_LRS or Premium_LRS
$storageType = 'Premium_LRS'
#Retrieve the Name of the Virtual Machine
$vmNames = Get-content " Patch of the text file which contains VM names"
Start-Transcript -path " Path where you want to save the Log file "
foreach ($vmName in $vmNames) {
# Stop the VM before changing the Size/Disktype
Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force
$vm = Get- AzVM -Name $vmName -resourceGroupName $rgName
# Not all sizes supports Premium Disk and the VMSize has to be upgraded. Skip the below if it not suits your
#$vm.HardwareProfile.VMSize = $size
#Update-AzVM -VM $vm -ResourceGroupName $rgName
# Get all disks that belong to the selected VM, convert to Premium Disk
$vmDisks = Get-AzDisk -ResourceGroupName $rgName
# For disks that belong to the selected VM, Convert to Premium Disk
foreach ($disk in vmDisks)
{
if($disk_ManagedBy -eq $vm.Id)
{
$disk.Sku = [Microsoft.Azure.Management.Compute.Models.DiskSku]::new($storageType)
$disk | Update-AzDisk
}
}
Start-AzVM -ResourceGroupName $rgName -Name $vmName
Write-Output $vmName | Out-File 'Path to the log File' -Append
}
Stop-Transcript
0 Comments
No Comments