Creating virtual machines using Azure PowerShell cmdlets

You can create virtual machines using Azure PowerShell cmdlets, including advanced configuration options such as: network endpoints, data disks, and Active Directory domain join information. You have full control over the names and locations of the underlying VHD (virtual hard disk) files for the virtual machine during creation. Like the Azure cross-platform command-line tools, the  Azure PowerShell cmdlets support most Azure resources for automation. The Azure PowerShell cmdlets only work on Windows clients.

There are two methods of creating a virtual machine using the Azure PowerShell cmdlets.

The first method is to use the New-AzureQuickVM cmdlet. This cmdlet allows you to provisiona single virtual machine with a simple configuration based on an image only. This cmdlet does not support instantiating a virtual machine from a disk. You also cannot specify additional endpoints, or create data disks during virtual machine creation.

The Windows PowerShell following example uses the New-AzureQuickVM cmdlet to create a virtual machine with the instance size set to Small. It is created in the contoso-vms cloud service in the West US region. The image name is retrieved using the Get-AzureVMImage cmdlet.

$adminUser = “[admin user name]”

$password = “[admin password]”

$serviceName = “contoso-vms”

$location = “West US”

$size = “Small”

$vmName = “vm1”

$imageFamily = “Windows Server 2012 R2 Datacenter”

$imageName = Get-AzureVMImage |

where { $_.ImageFamily -eq $imageFamily } |

sort PublishedDate -Descending |

select -ExpandProperty ImageName -First 1

New-AzureQuickVM -Windows `

-ServiceName $serviceName `

-Name $vmName `

-ImageName $imageName `

-AdminUsername $adminUser `

-Password $password `

-Location $location `

-InstanceSize $size

The second method uses a combination of cmdlets to build up a configuration object for the virtual machine. With this technique you can add additional options to the virtual machine at creation time such as data disks, endpoints, and Active Directory domain join information. The first cmdlet to call is New-AzureVMConig, which creates a local configuration object that can be passed to other Azure cmdlets that support the VM parameter either directly, or using the Windows PowerShell pipeline operator. With this method you can set operating system and data disk configuration (custom paths and file names), configure endpoints (including load-balanced), as well as advanced network features such as reserved, instance level, and static IP addresses.

The Windows PowerShell following example creates a Small virtual machine named vm2 in the contoso-vms cloud service. It also configures the new virtual machine to have an endpoint open for TCP port 1433 using the Add-AzureEndpoint cmdlet, and a 10 GB data disk attached using the Add-AzureData disk cmdlet.

$adminUser = “[admin user name]”

$password = “[admin password]”

$serviceName = “contoso-vms”

$location = “West US”

$size = “Small”

$vmName = “vm2”

$imageFamily = “Windows Server 2012 R2 Datacenter”

$imageName = Get-AzureVMImage |

where { $_.ImageFamily -eq $imageFamily } |

sort PublishedDate -Descending |

select -ExpandProperty ImageName -First 1

New-AzureVMConfig -Name $vmName `

-InstanceSize $size `

-ImageName $imageName |

Add-AzureProvisioningConfig -Windows `

-AdminUsername $adminUser `

-Password $password |

Add-AzureDataDisk -CreateNew `

-DiskSizeInGB 10 `

-LUN 0 `

-DiskLabel “data” |

Add-AzureEndpoint -Name “SQL” `

-Protocol tcp `

-LocalPort 1433 `

-PublicPort 1433 |

New-AzureVM -ServiceName $serviceName `

-Location $location

In the above example, the virtual machine configuration is created using the New-AzureVMConfig cmdlet, and passed to each of the cmdlets using the PowerShell pipeline operator. This can also be accomplished by storing the returned configuration in a variable and passing it with the VM parameter (Add-AzureEndpoint -VM $config), or by piping the returned variable itself to the cmdlet ($conig | Add-AzureEndpoint). Variations may show up on the exams so it is important to be familiar with each syntax. There are several additional cmdlets used in this example that are not available with New-AzureQuickVM. The Add-AzureProvisioningConig cmdlet is used to specify the configuration information needed when provisioning from an image such as machine name, and

the administrator user name and password. This cmdlet can modify the behavior of the virtual machine in several other ways. The Add-AzureDataDisk cmdlet is used to attach new empty disks, or attach existing data disks, to a virtual machine. The Add-AzureEndpoint cmdlet is used to create an input endpoint on the virtual machine. Input endpoints allow network trafic into the virtual machine on a specific port. This cmdlet can also be used to add load-balanced endpoints, and can attach access control lists.

To create the virtual machine using this technique you must pass the configuration object to the New-AzureVM cmdlet. The New-AzureVM cmdlet does the bulk of the work of creating the virtual machine with the passed in configuration. Table below shows the differences between the two approaches for creating virtual machines.

New-AzureQuickVMNew-AzureVMConfig and New-AzureVM
Windows and Linux supportedWindows and Linux supported
Create only from imageCreate from image or operating system disk
Specify availability set nameSpecify availability set name
Specify subnet and virtual networkSpecify subnet and virtual network
Deploy to location or afinity groupDeploy to location or afinity group
Deploy X509 certiicatesDeploy X509 certiicates
Deploy SSH certiicates on Linux virtual machinesDeploy SSH certiicates on Linux virtual machines
Specify Active Directory domain join information
Require admin password reset on first login
Create new or attach existing data disks
Configure endpoints (including internal and external load balancing)
Disable Windows Update
Specify the time zone
Specify static IP addresses
Specify reserved IP address of the cloud service/domain name

This article is a part of 70-533 Implementing Microsoft Azure Infrastructure Solutions Prep course.

More Articles included in this course are

Creating an Azure website

Creating an Azure website using  Portal, Powershell and CLI Creating an Azure website on Microsoft Azure Cloud starts with creating ...
Read More

Deployment slots in Azure website

Defining deployment slots in Azure website Deployment slots in Azure website are collection of individual Azure website linked to your ...
Read More

Publishing an Azure website

Publishing an Azure website using various methods For publishing an Azure website, the web application or code needs to be ...
Read More

WebJobs in Azure website

Introduction to WebJobs in Azure website and Deployment using Portal and Powershell Web Jobs in Azure websites are exclusive applications ...
Read More

Azure website settings

Introduction to Configuring the azure website settings The first step you will need to follow for an Azure website is to ...
Read More
Loading...

Practice Tests in this course are

70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 1

Instructions for Exam 70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 1 This page shows the instructions for Exam 70-533 ...
Read More

70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 2

Instructions for Exam 70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 2 This page shows the instructions for Exam 70-533 ...
Read More

70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 3

Instructions for Exam 70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 3 This page shows the instructions for Exam 70-533 ...
Read More

70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 4

Instructions for Exam 70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 4 This page shows the instructions for Exam 70-533 ...
Read More

70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 5

Instructions for Exam 70-533 Implementing Microsoft Azure Infrastructure Solutions Practice Test 5 This page shows the instructions for Exam 70-533 ...
Read More
Loading...