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)

Basic management tasks

Day-to-day operations will often require you to perform several management tasks, such as starting, stopping, and deleting vVMs. In addition, you may find yourself in situations where you will need (or want) to create scripts to automate certain VM tasks.

By becoming familiar with how to use Azure PowerShell, you can script and automate many common management tasks that would otherwise require manual intervention.

The five main commands I'm going to cover in this section are Stop-AzureRmVM, Start-AzureRmVM, Restart-AzureRmVM , Remove-AzureRmVM, and Remove-AzureRmResourceGroup.

The Stop VM command

To stop an Azure VM, you can use the Stop-AzureRmVm command. Go ahead and run the command to stop the myVM virtual machine:

Stop-AzureRmVm -ResourceGroupName "VMLab" -Name "myVM" -Force

The only switches required in the preceding command are the ResourceGroupName switch and the Name switch. The Force switch is optional, but it shuts down the VM without asking for confirmation.

Another optional switch (not shown) is the StayProvisioned switch. If you wish to stop a VM but keep the virtual machine allocated, you could specify the StayProvisioned switch as well. Keep in mind that if you do this, you will continue to be charged for compute cycles even though the VM is off.

Once you've stopped the myVM virtual machine, run the Get-AzureRmVM command to confirm the status of it:

Get-AzureRmVM `
-ResourceGroupName "VMLab" `
-Name "myVM" `
-Status

The preceding command will display the status of the myVM virtual machine and should show the status as Deallocated:

In the next section, you will start the myVM virtual machine, using the Start-AzureRmVM command.

The Start VM command

To start a VM, you can use the Start-AzureRmVM command. Run the following command to start the myVM virtual machine:

Start-AzureRmVM -ResourceGroupName "VMLab" -Name "myVM"

The preceding command starts the myVM virtual machine in the VMLab resource group.

Check the status using Get-AzureRmVM as before:

Get-AzureRmVM `
-ResourceGroupName "VMLab" `
-Name "myVM" `
-Status

The preceding command will display the status of the myVM virtual machine.

The Restart VM command

To restart a VM, you can use the Restart-AzureRmVM command. Run the following command to restart the myVM virtual machine:

Restart-AzureRmVM -ResourceGroupName "VMLab" -Name "myVM"

The preceding command restarts the myVM virtual machine in the VMLab resource group.

Check the status using Get-AzureRmVM:

Get-AzureRmVM `
-ResourceGroupName "VMLab" `
-Name "myVM" `
-Status

The preceding command will display the status of the myVM virtual machine.

The Remove VM command

There will be times when you need to decommission a VM. Doing so is performed with the Stop-AzureRmVM command. However, before deleting a VM, it should first be stopped. In this example, we'll stop the myVM virtual machine and then delete it.

To stop the VM, run the following command:

Stop-AzureRmVM -ResourceGroupName "VMLab" -Name "myVM" -Force

Once the VM is stopped, run the preceding Remove-AzureRmVM command to delete the VM:

Remove-AzureRmVM -ResourceGroupName "VMLab" -Name "myVM"

As was the case with the stop and start commands, only the ResourceGroupName and Name switches need to be provided:

After a few minutes, run the following command to see whether the myVM virtual machine has been deleted:

Get-AzureRmVM -status

If you've successfully deleted the VM, it should not be shown in the output of the preceding command.

The only hassle with the Remove-AzureRmVM command is that it only removes the VM. It does not clean up other resources that are attached to the VM, such as NICs or additional disks. They need to be cleaned up manually, unless you opt to script their removal.

The Remove resource group command

The final command that I want to cover is the Remove-AzureRmResourceGroup command. This command removes an entire resource group. I personally use it quite regularly, since I'm always creating lab-and course-specific resource groups. Once I'm done with the labs and courses, I just delete the resource groups so that all resources inside the resource groups in their entirety are removed simultaneously. This makes my life easier.

To remove the entire VMLab resource group, run the Remove-AzureRmResourceGroup command. Don't worry; we'll be re-creating it later in this book:

Remove-AzureRmResourceGroup -Name "VMLab" -Force

Running this command tells Azure to delete the VMLab resource group and everything inside it. The Force switch suppresses the are you sure prompts.

Although this command can sometimes take a while to complete, when it does complete, the entire resource group is removed, along with everything inside of it.

After running the preceding command (and waiting a few minutes), run the following Get-AzureRmResourceGroup command to confirm that the VMLab resource group is gone:

Get-AzureRmResourceGroup -Name "VMLab"
When operating in a production environment, you need to be careful when deleting resource groups. It's very easy to get into a lot of trouble if you aren't careful.

Your turn

Apply what you've learned in this chapter by completing the following project:

  • Project: Common Commands
  • Project goal: The goal of this project is to successfully deploy a VM and to practice stopping, starting, and removing it
  • Key commands:
    • New-AzureRmResourceGroup
    • Get-AzureRmResourceGroup
    • New-AzureRmVm
    • Get-AzureRmVM
    • Stop-AzureRmVM
    • Start-AzureRmVM
    • Remove-AzureRmVM
    • Remove-AzureRmResourceGroup
  • General steps:
    1. Create a resource group
    2. Deploy a VM with a default configuration
    3. Stop the VM
    4. Start the VM
    5. Delete the VM
    6. Delete the resource group and all its resources
  • Validation: Ensure that the VM deploys, and that it stops and starts as expected
  • Reference info: The Basic Management Tasks section