Monday, December 14, 2015

Update Azure VM Size in PowerShell for ARM aka V2

#login to Rm Account
Login-AzureRmAccount

#alternative
#$vmname = "centosweb1hdicp"

#select the subscription id
$subscriptionId =
    (Get-AzureRmSubscription |
     Out-GridView `
        -Title "Select an Azure Subscription ..." `
        -PassThru).SubscriptionId

Select-AzureRmSubscription `
    -SubscriptionId $subscriptionId

#select vm
$centosvm =
    (Get-AzureRmVM |
     Out-GridView `
        -Title "Select an Azure Resource Group ..." `
        -PassThru)

#select the resource group
$rgName = $centosvm.ResourceGroupName
#select the resource group/alternative
#$rgName =
#    (Get-AzureRmResourceGroup |
#     Out-GridView `
#        -Title "Select an Azure Resource Group ..." `
#        -PassThru).ResourceGroupName


#collect VM / alternative
#$centosvm = Get-AzureRmVM | Where-Object {$_.Name -eq "$vmname"}

# current size
$currVMSize=$centosvm.HardwareProfile.VirtualMachineSize
Write-Host "Current Size of the VM: $currVMSize" -BackgroundColor Red -ForegroundColor Cyan

#collect location
$location=(Get-AzureRmResourceGroup -Name $rgName).Location

#collect new size
$newVMSize =
    (Get-AzureRmVMSize -Location $location |
     Out-GridView `
        -Title "Select a VM Size ..." `
        -PassThru).Name

# Stop VM
$centosvm | Stop-AzureRmVm -Force

# Resize VM
$centosvm.HardwareProfile.VirtualMachineSize = $newVMSize
$centosvm | Update-AzureRmVM

# Start VM
$centosvm | Start-AzureRmVm



2 comments: