Book Image

Azure PowerShell Quick Start Guide

By : Thomas Mitchell
Book Image

Azure PowerShell Quick Start Guide

By: Thomas Mitchell

Overview of this book

As an IT professional, it is important to keep up with cloud technologies and learn to manage those technologies. PowerShell is a critical tool that must be learned in order to effectively and more easily manage many Azure resources. This book is designed to teach you to leverage PowerShell to enable you to perform many day-to-day tasks in Microsoft Azure. Taking you through the basic tasks of installing Azure PowerShell and connecting to Azure, you will learn to properly connect to an Azure tenant with PowerShell. Next, you will dive into tasks such as deploying virtual machines with PowerShell, resizing them, and managing their power states with PowerShell. Then, you will learn how to complete more complex Azure tasks with PowerShell, such as deploying virtual machines from custom images, creating images from existing virtual machines, and creating and managing of data disks. Later, you will learn how to snapshot virtual machines, how to encrypt virtual machines, and how to leverage load balancers to ensure high availability with PowerShell. By the end of this book, you will have developed dozens of PowerShell skills that are invaluable in the deployment and management of Azure virtual machines.
Table of Contents (7 chapters)

Resizing a VM with PowerShell

There will be times when an application will require more resources than it once did. In cases such as these, an Azure VM can be resized.

Resizing a VM requires the use of two different PowerShell commands. The first command, called Get-AzureRmVMSize, is used to retrieve a list of VM sizes available in a chosen region. Before resizing a virtual machine, you must ensure that whatever size you want to change the virtual machine to is available in the virtual machine's region. The Get-AzureRmVMSize command does exactly this.

The second command that is required the Update-AzureRmVM command. This command is used to update a VM after changing its size configuration.

Resizing a VM

In this section, you are going to resize the myVM virtual machine that you created earlier. However, before attempting to do so, you will need to retrieve a list of VM sizes available in the EastUS region, which is where the myVM virtual machine resides.

To complete this task, run the following command from PowerShell:

Get-AzureRmVMSize -Location "EastUS"

The preceding command should return quite a few sizes. Confirm that Standard DS2_V2 is available. If it's not, find another size that is available.

After confirming that Standard DS2_V2 (or your chosen size) is available, you can use PowerShell to resize the myVM virtual machine:

After confirming that Standard DS2_V2 is available in the EastUS region, you also need to make sure that it's available on the current cluster where the myVM virtual machine resides. To do so, use the same Get-AzureRmVMSize command as before—just with a few different switches.

To confirm that the Standard DS2_V2 size (or your chosen size) is available on the cluster where myVM resides, run the following command:

Get-AzureRmVMSize -ResourceGroupName "VMLab" -VMName "myVM"

By specifying the -VMName and -ResourceGroupName switches in the preceding command, you can confirm that the Standard DS2_V2 size is available on the same cluster as the VM. If so, the virtual machine can be resized from a powered-on state (without the need to deallocate it). However, it will still require a reboot during the operation:

If the Standard DS2_V2 size is not available on the VM's current cluster, the virtual machine needs to first be deallocated before the resize operation can occur. In such a case, any data on the temp disk is removed and the public IP address will change (unless a static IP address is being used).

To resize the virtual machine (myVM), you need to run a few different PowerShell commands.

The first command essentially loads the configuration profile of the VM into a variable called $vm. The second command will modify the -VMSize attribute stored in that variable to reflect the new VM size. The third command, called Update-AzureRmVM, will take the updated configuration that is stored in the $vm variable and write it to the VM.

To begin the resize process of the myVM virtual machine, run the following command from a PowerShell session that's connected to your Azure tenant:

$vm = Get-AzureRmVM -ResourceGroupName "VMLab" -VMName "myVM"

The preceding command retrieves the current VM information and stores it in the $vm variable.

To modify the size attribute for the VM, run the following command:

$vm.HardwareProfile.VmSize = "Standard_DS2_V2"

The previous command changes the size attribute of the myVM virtual machine that's stored in the variable to Standard DS2_V2.

Once the new size has been specified using the preceding command, run the following command to update the VM:

Update-AzureRmVM -VM $vm -ResourceGroupName "VMLab"

After executing the preceding Update-AzureRmVM command, the myVM virtual machine is updated and then automatically restarted:

The process takes a few minutes to complete, but when it completes, the myVM virtual machine is resized to reflect the new size.