Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Azure PowerShell Quick Start Guide

You're reading from  Azure PowerShell Quick Start Guide

Product type Book
Published in Dec 2018
Publisher Packt
ISBN-13 9781789614954
Pages 118 pages
Edition 1st Edition
Languages
Author (1):
Thomas Mitchell Thomas Mitchell
Profile icon Thomas Mitchell

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
You have been reading a chapter from
Azure PowerShell Quick Start Guide
Published in: Dec 2018 Publisher: Packt ISBN-13: 9781789614954
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}