Skip to content

Instantly share code, notes, and snippets.

@darkshade9
Last active March 9, 2017 17:02
Show Gist options
  • Select an option

  • Save darkshade9/888f633032b3cb9045ed724b5a6ff9be to your computer and use it in GitHub Desktop.

Select an option

Save darkshade9/888f633032b3cb9045ed724b5a6ff9be to your computer and use it in GitHub Desktop.
Scale Azure VM Up or Down
#Param needs to be either 'up' or 'down'
#scaleSize is optional if you know EXACTLY the name of the VM size you want
param(
[Parameter(Mandatory = $true)]
[String] $scale,
[Parameter(Mandatory = $false)]
[String] $scaleSize
)
$subscrip = "SUBSCRIPTIONNAME"
$AzureCred = Get-AutomationPSCredential -Name 'AUTOMATION_ACCOUNT'
Login-AzureRmAccount -Credential $AzureCred -ErrorAction Stop | Out-Null
Write-Host "Selecting Subscription: $($subscrip)"
Select-AzureRmSubscription -SubscriptionName $subscrip -ErrorAction Stop | Out-Null
# Variables
$ResourceGroupName = "RESOURCEGROUPNAME"
# Select the VMs you want to scale. This example checks for a specific naming scheme for your VMs
# i.e. replace *SCALEME* with *iisserver* will scale all VMs with the names containing "iisserver"
$getVMName = Get-AzureRmVM | Where ({$_.name -notlike "DONTSCALEME1" -and $_.name -notlike "DONTSCALEME2" -and $_.name -like "*SCALEME*"})
$VMName = $getVMName
$vmArray = $VMName.name
# If $scaleSize is not set, define default values
if(!$scaleSize){
$smallVMsize = "Standard_F1S"
$bigVMsize = "Standard_F8S"
}
if ($scale -eq "DOWN"){
$VMSize = $smallVMsize
} elseif ($scale -eq "UP"){
$VMSize = $bigVMsize
} else {
"Error: Input needs to be UP or DOWN"
exit 1
}
# Ignores 'up' or 'down', just sets VMSize to user input via $scaleSize
if($scaleSize){
$VMSize = $scaleSize
}
$starttime = get-date -format hh:mm:ss
workflow resizeVMs {
param ([string[]]$vmArray)
foreach -parallel ($VMName in $vmArray){
"I am scaling $VMName $scale to $VMSize !"
"Starting resize at $starttime"
# Shut down VM: VM must be shut down before size change to switch between A/D/DS/Dv2/G/GS/N
Stop-AzureRmVm -name $VMName -ResourceGroupName $ResourceGroupName -StayProvisioned -Force
}
}
foreach ($VMName in $vmArray){
# Resize VM
$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName
$vm.HardwareProfile.VmSize = $VMSize
Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $vm
$endtime = get-date -format hh:mm:ss
$time = New-TimeSpan -Start $starttime -End $endtime
# Start VM
Start-AzureRmVm -name $VMName -ResourceGroupName $ResourceGroupName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment