Last active
April 18, 2020 10:33
-
-
Save raandree/f997e3c2f902f6ecec19a670a885da89 to your computer and use it in GitHub Desktop.
Changes the Sku of all disks connected to a VM to the desired one. Chaning the VMs role size might also be required.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory)] | |
$ResourceGroupName, | |
[Parameter(Mandatory)] | |
$VmName, | |
[Parameter(Mandatory)] | |
[ValidateSet('Standard_LRS', 'Premium_LRS', 'StandardSSD_LRS', 'UltraSSD_LRS')] | |
$StorageType, | |
[Parameter(Mandatory)] | |
$VmSize, | |
[switch]$SkipVmSizeCheck, | |
[switch]$StartVmAfterChange | |
) | |
function Get-AzureAvailableRoleSize | |
{ | |
param | |
( | |
[Parameter(Mandatory)] | |
[string]$Location | |
) | |
if (-not (Get-AzContext -ErrorAction SilentlyContinue)) | |
{ | |
Connect-AzAccount | Out-Null | |
} | |
$azLocation = Get-AzLocation | Where-Object Location -eq $Location | |
$availableRoleSizes = Get-AzComputeResourceSku | Where-Object { | |
$_.ResourceType -eq 'virtualMachines' -and $_.Locations -contains $azLocation.Location -and $_.Restrictions.ReasonCode -notcontains 'NotAvailableForSubscription' | |
} | Select-Object -ExpandProperty Name | |
Get-AzVMSize -Location $Location | Where-Object -Property Name -in $availableRoleSizes | |
} | |
$resourceGroup = Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction Stop | |
if (-not $SkipVmSizeCheck) | |
{ | |
$availableRoleSizes = AzureAvailableRoleSize -Location $resourceGroup.Location | |
if ($VmSize -notin $availableRoleSizes.Name) | |
{ | |
Write-Error "The role size '$VmSize' is not available in the location '$($resourceGroup.Location)'" | |
return | |
} | |
} | |
$vm = Get-AzVM -Name $VmName -resourceGroupName $ResourceGroupName -Status -ErrorAction Stop | |
if ($vm.Statuses | Where-Object Code -eq PowerState/deallocated) | |
{ | |
Write-Host "VM '$VmName' is already deallocated" | |
} | |
else | |
{ | |
Write-Host "VM '$VmName' is running, stopping and deallocating it." | |
Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VmName -Force | |
} | |
$vm = Get-AzVM -Name $VmName -resourceGroupName $ResourceGroupName | |
# Change the VM size to a size that supports Premium storage | |
# Skip this step if converting storage from Premium to Standard | |
$vm.HardwareProfile.VmSize = $VmSize | |
if ($vm.HardwareProfile.VmSize -ne $VmSize) | |
{ | |
Write-Host "Changing VM size from '$($vm.HardwareProfile.VmSize)' to '$VmSize'." | |
Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName | Out-Null | |
} | |
else | |
{ | |
Write-Host "VM does already have the desired size of '$($vm.HardwareProfile.VmSize)'." | |
} | |
# Get all disks in the resource group of the VM | |
$vmDisks = Get-AzDisk -ResourceGroupName $ResourceGroupName | |
Write-Host "Found $($vmDisks.Count) disks in the resource group '$ResourceGroupName'" | |
# For disks that belong to the selected VM, convert to Premium storage | |
foreach ($disk in $vmDisks) | |
{ | |
if ($disk.ManagedBy -eq $vm.Id) | |
{ | |
if ($disk.Sku.Name -ne $StorageType) | |
{ | |
Write-Host "`tUpdating disk '$($disk.UniqueId)' from '$($disk.Sku.Name)' to '$StorageType'." | |
$disk.Sku = [Microsoft.Azure.Management.Compute.Models.DiskSku]::new($StorageType) | |
$disk | Update-AzDisk | Out-Null | |
} | |
else | |
{ | |
Write-Host "`tDisk '$($disk.UniqueId)' is already '$($disk.Sku.Name)'." | |
} | |
} | |
} | |
if ($StartVmAfterChange) | |
{ | |
Write-Host "Starting VM '$VmName'" | |
Start-AzVM -ResourceGroupName $ResourceGroupName -Name $VmName | |
} | |
Write-Host "Finished updating VM '$VmName'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment