0

I want to change the size an Azure VM with powershell. The reason is: I use machine for development. I need A2 size for 4 hours a day. The owner of the VM asked to switch the size of the machine to A0 when I do not develop. I have access to the Azure subscription with my MS Live account. Now I change the size manually through Azure Portal. I want to automate this task with PowerShell. The script should set the size to A2, wait for 4 hours and set it back to A0. I just want to doubleclick the script before starting my development and just forget about the question.

I have the following understanding of the general procedure:

  1. Run Import-AzurePublishSettings
  2. Run Select-AzureSubscription
  3. Get VM object with Get-AzureVM
  4. Run Set-AzureVMSize
  5. Update-AzureVM

I can not get publish profile, because I do not own the machine. Is there a way to authenticate with MS Live account?

1 Answer 1

1

Skip the Import-Azurepublish and do a Add-AzureAccount instead. That will popup UI for authenticating with your MS Live account.

Once that is done you can use Select-AzureSubscription

For Classic Deployment you need this:

# authenticate if no account is already added to the powershell session
if (!(Get-AzureAccount)){ Add-AzureAccount }

# Get the vm object out of azure
$vm = get-azurevm | where name -eq "name of the vm"

# Now all you need is to is update the VM with its new size:

$vm | Set-AzureVMSize -InstanceSize Medium | Update-AzureVM

If the VM is deployed via the Resource Manager (RM Model)

if (!(Get-AzureRMContext)){ Add-AzureRmAccount } 
Select-AzureRmSubscription -SubscriptionId "{subscriptionId}" 

$vm = Get-AzureRmVm | where name -eq "{vmName}" 
$vm.HardwareProfile.vmSize = "Medium" 
Update-AzureRmVM -VM $vm 

btw. Medium is what A2 is called in the API.

Sign up to request clarification or add additional context in comments.

5 Comments

No problem, I will try and put some code together when I am near my laptop later today...
I edited the original answer so it now have some sample code in it for the steps you need to take.
Thank you! I used your code as a starting point. FInally this worked for me: if (!(Get-AzureAccount)){ Add-AzureRmAccount } Select-AzureRmSubscription -SubscriptionId "{subscriptionId}" $vm = Get-AzureRmVm | where name -eq "{vmName}" $vm.HardwareProfile.vmSize = "{size}" Update-AzureRmVM -VM $vm I had to install AzureRM, otherwise it did not work. Please, prepend this to your answer, so I can mark it as an answer
Also note that the code you have posted has a bug in it, it checks if the classic model is logged in, if not it logs in to the RM model. I fixed that in the in the above posted example.
Oh, great, I was wondering why it keeps asking me each time for my credentials ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.