public/Set-AzureVMSize.ps1

function Set-AzureVMSize {

<#
 
.SYNOPSIS
Resize existing AzureVM
 
.DESCRIPTION
Set-AzureVMSize cmdlet resizes an existing Azure VM to a more appropriate SKU.
 
.PARAMETER VMname
specify the VMname you want to resize
 
.PARAMETER NewVMSize
specify the new VMsize for resize
 
.PARAMETER subscriptionName
specify the subscription name where the VM resides.
 
.EXAMPLE
Set-AzureVMSize -VMName <VMName> -NewVMSize <VMsize> -subscriptionName <subscriptionName> -Verbose
 
 
#>


[cmdletbinding()]

param (
    
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]$VMName,
    
    [Parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string]$NewVMSize,
    
    [Parameter(Mandatory=$true)]
    [string]$subscriptionName

)

    Process 
    {

        # check to see if local token exists (ran Login-AzureRMAccount)
        if (($null -eq (Get-AzureRmContext).Account)) {
            Write-Warning "Please run < Login-AzureRMAccount > first to create a session token...exiting."
            break
        } 

        # Get subscription name
        Try {
            Select-AzureRmSubscription -SubscriptionName $subscriptionName -ErrorAction Stop -Verbose | Out-Null
        }
        Catch {
            $error[0].Exception
            break
        }

        Try {
            # couldn't explicitly get the vm for whatever reaso by passing the rg and vm name's as string....so doing the below :( I <3 Microsoft.
            $vm = Get-AzureRmResource -ErrorAction Stop -Verbose | 
            Where-Object {$_.name -like $VMName -and $_.resourcetype -eq 'Microsoft.Compute/virtualMachines'}
            $vm = $vm | Get-AzureRmVM

            if ($null -eq $VM) {
                Write-Warning "VM cannot be found, ensure you've referenced the correct subscription, or check your spelling and try again..."
                break
            }

            $vm.HardwareProfile.VmSize = $NewVMSize
        }
        Catch {
            $error[0].Exception
            break
        }
        
        Try {
            $results = $vm | Update-AzureRmVM -ErrorAction Stop -Verbose
        }
        Catch {
            $error[0].Exception
            break
        }

        return $results

    } #end process block

}