Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
196 views

Create and Manage Windows VMs With Azure PowerShell

This document provides a tutorial on how to perform basic tasks for managing Windows virtual machines (VMs) with Azure PowerShell, such as creating and connecting to VMs, selecting VM images, viewing VM sizes, resizing VMs, and understanding VM states. It covers how to create a resource group, deploy VMs using images from the Azure Marketplace, connect to VMs remotely, find available VM sizes, and resize existing VMs.

Uploaded by

darioime
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views

Create and Manage Windows VMs With Azure PowerShell

This document provides a tutorial on how to perform basic tasks for managing Windows virtual machines (VMs) with Azure PowerShell, such as creating and connecting to VMs, selecting VM images, viewing VM sizes, resizing VMs, and understanding VM states. It covers how to create a resource group, deploy VMs using images from the Azure Marketplace, connect to VMs remotely, find available VM sizes, and resize existing VMs.

Uploaded by

darioime
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Tutorial: Create and Manage Windows

VMs with Azure PowerShell

Azure virtual machines provide a fully configurable and flexible computing


environment. This tutorial covers basic Azure virtual machine (VM) deployment tasks
like selecting a VM size, selecting a VM image, and deploying a VM. You learn how to:

 Create and connect to a VM


 Select and use VM images
 View and use specific VM sizes
 Resize a VM
 View and understand VM state
Launch Azure Cloud Shell
The Azure Cloud Shell is a free interactive shell that you can use to run the steps in
this article. It has common Azure tools preinstalled and configured to use with your
account.

To open the Cloud Shell, just select Try it from the upper right corner of a code
block. You can also launch Cloud Shell in a separate browser tab by going
to https://shell.azure.com/powershell. Select Copy to copy the blocks of code, paste
it into the Cloud Shell, and press enter to run it.

Create resource group


Create a resource group with the New-AzResourceGroup command.

An Azure resource group is a logical container into which Azure resources are
deployed and managed. A resource group must be created before a virtual machine.
In the following example, a resource group named myResourceGroupVM is created in
the EastUS region:

Azure PowerShellCopy
Try It
New-AzResourceGroup `
-ResourceGroupName "myResourceGroupVM" `
-Location "EastUS"

The resource group is specified when creating or modifying a VM, which can be seen
throughout this tutorial.
Create a VM
When creating a VM, several options are available like operating system image,
network configuration, and administrative credentials. This example creates a VM
named myVM, running the default version of Windows Server 2016 Datacenter.

Set the username and password needed for the administrator account on the VM
with Get-Credential:

Azure PowerShellCopy
Try It
$cred = Get-Credential

Create the VM with New-AzVM.

Azure PowerShellCopy
Try It
New-AzVm `
-ResourceGroupName "myResourceGroupVM" `
-Name "myVM" `
-Location "EastUS" `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-SecurityGroupName "myNetworkSecurityGroup" `
-PublicIpAddressName "myPublicIpAddress" `
-Credential $cred

Connect to VM
After the deployment has completed, create a remote desktop connection with the
VM.

Run the following commands to return the public IP address of the VM. Take note of
this IP Address so you can connect to it with your browser to test web connectivity in
a future step.

Azure PowerShellCopy
Try It
Get-AzPublicIpAddress `
-ResourceGroupName "myResourceGroupVM" | Select IpAddress

Use the following command, on your local machine, to create a remote desktop
session with the VM. Replace the IP address with the publicIPAddress of your VM.
When prompted, enter the credentials used when creating the VM.

PowerShellCopy
mstsc /v:<publicIpAddress>

In the Windows Security window, select More choices and then Use a different
account. Type the username and password you created for the VM and then click OK.

Understand marketplace images


The Azure marketplace includes many images that can be used to create a new VM.
In the previous steps, a VM was created using the Windows Server 2016 Datacenter
image. In this step, the PowerShell module is used to search the marketplace for
other Windows images, which can also be used as a base for new VMs. This process
consists of finding the publisher, offer, SKU, and optionally a version number
to identify the image.

Use the Get-AzVMImagePublisher command to return a list of image publishers:

Azure PowerShellCopy
Try It
Get-AzVMImagePublisher -Location "EastUS"

Use the Get-AzVMImageOffer to return a list of image offers. With this command, the
returned list is filtered on the specified publisher named MicrosoftWindowsServer:

Azure PowerShellCopy
Try It
Get-AzVMImageOffer `
-Location "EastUS" `
-PublisherName "MicrosoftWindowsServer"

The results will look something like this example:

PowerShellCopy
Offer PublisherName Location
----- ------------- --------
Windows-HUB MicrosoftWindowsServer EastUS
WindowsServer MicrosoftWindowsServer EastUS
WindowsServer-HUB MicrosoftWindowsServer EastUS

The Get-AzVMImageSku command will then filter on the publisher and offer name to
return a list of image names.

Azure PowerShellCopy
Try It
Get-AzVMImageSku `
-Location "EastUS" `
-PublisherName "MicrosoftWindowsServer" `
-Offer "WindowsServer"

The results will look something like this example:

PowerShellCopy
Skus Offer PublisherName
Location
---- ----- ------------- ---
-----
2008-R2-SP1 WindowsServer MicrosoftWindowsServer
EastUS
2008-R2-SP1-smalldisk WindowsServer MicrosoftWindowsServer
EastUS
2012-Datacenter WindowsServer MicrosoftWindowsServer
EastUS
2012-Datacenter-smalldisk WindowsServer MicrosoftWindowsServer
EastUS
2012-R2-Datacenter WindowsServer MicrosoftWindowsServer
EastUS
2012-R2-Datacenter-smalldisk WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter-Server-Core WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter-Server-Core-smalldisk WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter-smalldisk WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter-with-Containers WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter-with-Containers-smalldisk WindowsServer MicrosoftWindowsServer
EastUS
2016-Datacenter-with-RDSH WindowsServer MicrosoftWindowsServer
EastUS
2016-Nano-Server WindowsServer MicrosoftWindowsServer
EastUS

This information can be used to deploy a VM with a specific image. This example
deploys a VM using the latest version of a Windows Server 2016 with Containers
image.

Azure PowerShellCopy
Try It
New-AzVm `
-ResourceGroupName "myResourceGroupVM" `
-Name "myVM2" `
-Location "EastUS" `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-SecurityGroupName "myNetworkSecurityGroup" `
-PublicIpAddressName "myPublicIpAddress2" `
-ImageName "MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-
Containers:latest" `
-Credential $cred `
-AsJob

The -AsJob parameter creates the VM as a background task, so the PowerShell


prompts return to you. You can view details of background jobs with the Get-
Job cmdlet.

Understand VM sizes
The VM size determines the amount of compute resources like CPU, GPU, and
memory that are made available to the VM. Virtual machines should be created using
a VM size appropriate for the workload. If a workload increases, an existing virtual
machine can also be resized.

VM Sizes

The following table categorizes sizes into use cases.

Type Common sizes Description

General B, Dsv3, Dv3, DSv2, Balanced CPU-to-memory. Ideal for dev / test and small
purpose Dv2, Av2, DC to medium applications and data solutions.

Compute Fsv2 High CPU-to-memory. Good for medium traffic


optimized applications, network appliances, and batch processes.

Memory Esv3, Ev3, M, DSv2, High memory-to-core. Great for relational databases,
optimized Dv2 medium to large caches, and in-memory analytics.

Storage Lsv2, Ls High disk throughput and IO. Ideal for Big Data, SQL,
optimized and NoSQL databases.

GPU NV, NVv2, NC, Specialized VMs targeted for heavy graphic rendering
NCv2, NCv3, ND and video editing.

High H Our most powerful CPU VMs with optional high-


performance throughput network interfaces (RDMA).

Find available VM sizes


To see a list of VM sizes available in a particular region, use the Get-
AzVMSize command.

Azure PowerShellCopy
Try It
Get-AzVMSize -Location "EastUS"

Resize a VM
After a VM has been deployed, it can be resized to increase or decrease resource
allocation.

Before resizing a VM, check if the size you want is available on the current VM cluster.
The Get-AzVMSize command returns a list of sizes.

Azure PowerShellCopy
Try It
Get-AzVMSize -ResourceGroupName "myResourceGroupVM" -VMName "myVM"

If the size is available, the VM can be resized from a powered-on state, however it is
rebooted during the operation.

Azure PowerShellCopy
Try It
$vm = Get-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-VMName "myVM"
$vm.HardwareProfile.VmSize = "Standard_DS3_v2"
Update-AzVM `
-VM $vm `
-ResourceGroupName "myResourceGroupVM"

If the size you want isn't available on the current cluster, the VM needs to be
deallocated before the resize operation can occur. Deallocating a VM will remove any
data on the temp disk, and the public IP address will change unless a static IP address
is being used.

Azure PowerShellCopy
Try It
Stop-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-Name "myVM" -Force
$vm = Get-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-VMName "myVM"
$vm.HardwareProfile.VmSize = "Standard_E2s_v3"
Update-AzVM -VM $vm `
-ResourceGroupName "myResourceGroupVM"
Start-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-Name $vm.name

VM power states
An Azure VM can have one of many power states.

Power
State Description

Starting The virtual machine is being started.

Running The virtual machine is running.

Stopping The virtual machine is being stopped.

Stopped The VM is stopped. Virtual machines in the stopped state still incur compute charges.

Deallocating The VM is being deallocated.

Deallocated Indicates that the VM is removed from the hypervisor but is still available in the
control plane. Virtual machines in the Deallocated state do not incur compute charges.

- The power state of the VM is unknown.

To get the state of a particular VM, use the Get-AzVM command. Be sure to specify a
valid name for a VM and resource group.

Azure PowerShellCopy
Try It
Get-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-Name "myVM" `
-Status | Select @{n="Status"; e={$_.Statuses[1].Code}}

The output will look something like this example:

PowerShellCopy
Status
------
PowerState/running

Management tasks
During the lifecycle of a VM, you may want to run management tasks like starting,
stopping, or deleting a VM. Additionally, you may want to create scripts to automate
repetitive or complex tasks. Using Azure PowerShell, many common management
tasks can be run from the command line or in scripts.

Stop a VM

Stop and deallocate a VM with Stop-AzVM:

Azure PowerShellCopy
Try It
Stop-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-Name "myVM" -Force

If you want to keep the VM in a provisioned state, use the -StayProvisioned


parameter.

Start a VM
Azure PowerShellCopy
Try It
Start-AzVM `
-ResourceGroupName "myResourceGroupVM" `
-Name "myVM"
Delete resource group

Everything inside of a resource group is deleted when you delete the resource group.

Azure PowerShellCopy
Try It
Remove-AzResourceGroup `
-Name "myResourceGroupVM" `
-Force

Next steps
In this tutorial, you learned about basic VM creation and management such as how
to:

 Create and connect to a VM


 Select and use VM images
 View and use specific VM sizes
 Resize a VM
 View and understand VM state

Advance to the next tutorial to learn about VM disks.

You might also like