Summary: Use PowerShell cmdlets to start to create a virtual machine in Azure Resource Manager.
How do I create virtual machines (VMs) in Azure Resource Manager by using PowerShell? Where do I start?
Honorary Scripting Guy, Sean Kearney, is here, and we’re going to give you some basic step-by-step guidance this week about how to spin up a VM with Azure Resource Manager PowerShell cmdlets. We’re also going to reference back to some cool tricks that we picked up last week to obtain data from a previous VM so that you don’t have to go about digging for the settings.
When I go to prepare a VM in Azure Resource Manager (outside of the actual preparation of the infrastructure such as our network or resource groups), I tend to think of six key tasks:
- Set up the base configuration (VM size and name)
- Set up the storage for the operating system image
- Define the network configuration (virtual network, card, subnet, security group)
- Set up the operating-specific details (template for the image)
- Assign the user ID and password for the operating system
- Spin up the VM
When I think of these six small pieces and look at them within a script, you can actually visualize the layers of the VM falling together like a well-defined structure.
The other part that makes all of this far easier in Azure Resource Manager is knowing that you can create a VM (like we did last week) within the portal and then access its properties to know what to supply to PowerShell.
As you know, often it’s not about what parameters I need to use but more about what values are valid. Because we can get valid values from other VMs within Azure Resource Manager (even if the VMs aren’t powered up), it’s far easier on the IT Pro. You can do it in the Azure portal. If you need to replicate the solution multiple times, you can take a sample PowerShell script for a VM and plug in the appropriate values.
At this point we should actually start the process, shouldn’t we? Well then, release the cmdlets!
First, as always, we need to authenticate to Azure Resource Manager by using the Add-AzureRMAccount cmdlet. To give you a quick and simple reference, here is a small sample script. It contains the subscription ID and tenant ID and prompts for the LoginID.
$SubscriptionID='00000000-0000-0000-0000-000000000000'
$TenantID='00000000-0000-0000-0000-000000000000'
$UserID='AzureLoginID@Contoso.OnMicrosoft.com'
$Credential=Get-Credential $UserID
Add-AzureRMAccount -credential $Credential -tenantid $TenantID -subscriptionid $SubscriptionID
Now that we’re authenticated, we can start to do the first big piece: start up the configuration.
The first cmdlet that we’re going to use is the New-AzureRMVMConfig cmdlet. Its task is actually quite simple: to lay down the very first piece of our VM definition.
The cmdlet will need two parameters at a minimum:
- The VM (and by proxy, the computer name)
- The machine size
If you have an availability set defined, which is a key piece that you need to provide redundancy in Azure, you can provide it here.
The VM size is pretty critical depending on your need and your loads. A complete description and sizing chart is at Sizes for virtual machines in Azure.
The easy answer for the size to choose is, “It depends.” The amazing answer to everything from a consultant.
Put these words in your head to determine the answer:
- Budget
- Availability
- Performance
In some cases, you can have a very expensive environment that only starts up and shuts down for hours, days, or even a month. You can also have several smaller solutions that are spread across different locations. This can take some thought.
For our reference, we’re going to presume that you’re working with your last VM as a reference and are satisfied with the size.
From last week, we obtained the size of our previous VM by grabbing the VM object from Azure Resource Manager and obtaining the size property directly.
# Get Virtual Machine Object from AzureRM
$VM=Get-AzureRMVM –ResourceGroupName HSG-AzureRG –Name HSG-Linux1
# Retrieve Virtual Machine Size from Object
$VMSize=$vm.HardwareProfile.VmSize
In the case of this particular VM, I piped the results to the CLIP command to store it on the clipboard and pasted it into my CreateAzureVM.ps1 Script.
$VMSize | Clip
One CTRL-V or mouse click later…
$VMSize=’ Standard_A0’
At this point, we choose a new VM name:
$VMName=’HSG-Server1’
Then, we define the configuration by using this cmdlet:
$VMConfig=New-AzureRMVMConfig –VMName $VMName –VMSize $VMSize
This really doesn’t do anything in Azure yet. It’s just defining an object so that it won’t trap for the size or the name.
But, trapping for the name wouldn’t be difficult. You could just run something simple like this first:
$VMFound=$NULL; $VMFound=Get-AzureRMVM –name $VMName –ResourceGroupName HSG-AzureRG
If ($VMFound)
{
Write-output “Virtual Machine name $VMName Already Exists”
}
Else
{
$VMConfig=New-AzureRMVMConfig –VMName $VMName –VMSize $VMSize
}
We have now started the base. Tomorrow, we’ll continue on with defining our storage for our new VM in Azure Resource Manager.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.
Until then, always remember that with Great PowerShell comes Great Responsibility.
Sean Kearney
Honorary Scripting Guy
Cloud and Datacenter Management MVP