Step #1: Connect to your Azure subscription ^
The first thing we need to do is sign in to Azure:
1
|
Add-AzureAccount
|
Next, you have to create a storage account because the VM image will be stored in Azure Storage:
1
|
New-AzureStorageAccount –StorageAccountName '150west' -Location 'West US'
|
Next, I use Set-AzureSubscription to set the new storage account as the current storage account for my Azure subscription. You can get the name of your subscription with Get-AzureSubscription; in the example below, mine is '150dollar':
1
2
|
Get-AzureSubscription
Set-AzureSubscription -SubscriptionName '150dollar' -CurrentStorageAccountName '150west'
|
Step #2: Find your desired OS image ^
We now programmatically access the Azure operating system (OS) image gallery to get the "latest and greatest" Windows Server 2012 R2 image.The following Get-AzureVMImage pipeline retrieves the most recent image name, however honkin' long it is. Once you have it, select it and copy it to your clipboard, because we'll need it in the next step.
1
|
Get-AzureVMImage | Select-Object -Property ImageFamily, ImageName, PublishedDate | Where-Object { $_.ImageFamily -like 'Windows Server 2012 R2*' } | Sort-Object -Property PublishedDate -Descending | Select-Object -First 1 | Format-List
|
- First, we retrieve all Azure VM images from the Microsoft gallery.
- Second, we extract three key properties from the original dataset (which is very large).
- Third, we filter to retrieve only images that start with 'Windows Server 2012 R2'.
- Fourth, we perform a descending sort on publication date. (Remember that our goal is to get the most recent image.)
- Fifth, we run Select-Object to reduce our results only to the most recent OS image.
- Sixth and finally, we put the view in list format for easier browsing.
Step #3: Create your VM configuration ^
We need to create a VM configuration before we use New-AzureVM to create the virtual machine.I'm a fan of storing settings in variables and then passing the variables into cmd-lets as parameter values.
1
2
3
|
$image = 'a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-20151022-en.us-127GB.vhd'
$vmname = 'dc1'
$vmsize = 'Small'
|
- $image: This variable stores the OS image name we found in a previous step. Note that the image name changes from time to time.
- $vmname: This is the DNS hostname of our new VM.
- $vmsize: This is the VM instance size. Be careful that you're choosing a size that is (a) correct for the VM you're deploying and (b) consistent with your spending budget.
1
|
$vm1 = New-AzureVMConfig -ImageName $image -Name $vmname -InstanceSize $vmsize
|
1
|
$cred = Get-Credential -Message 'Type the local administrator account username and password:'
|
1
|
$vm1 | Add-AzureProvisioningConfig -Windows -AdminUsername $cred.GetNetworkCredential().Username -Password $cred.GetNetworkCredential().Password
|
Step #4: Create the VM ^
First, we have to create a new cloud service, which provides a container for our server and assigns a unique public DNS name and IP to the server. The public DNS name consists of the service name plus coudapp.net. In our example, the DNS name would be 'west150service.coudapp.net'. Thus, the service name has to be unique in Azure.We then use the service name to create the VM with the New-AzureVM cmdlet:
1
2
3
|
$svcname='west150service'
New-AzureService -ServiceName $svcname -Label 'My label' -Location 'West US'
New-AzureVM –ServiceName $svcname -VMs $vm1 -WaitForBoot
|
We can then invoke Get-AzureVM to verify the state of our new VM:
1
2
3
4
5
|
Get-AzureVM
which results in:
ServiceName Name Status
----------- ---- ------
west150service dc1 ReadyRole
|

Creating our connection .RDP file
As you can see in the following screen capture, my DC1 virtual machine is up and running!
Our Azure cloud-based VM
A final note: If you're just testing, please make sure not only to stop but to deallocate your virtual machine, to ensure that you don't get charged by Microsoft for reserving computational power for your "simply powered off" VM. Sneaky, eh?You shut down and deallocate your VM by running Stop-AzureVM with the –Force switch.
In the next example, I'm using Where-Object to stop and deallocate all my running VMs:
1
|
Get-AzureVM | Where-Object {$_.Status -eq 'ReadyRole'} | Stop-AzureVM -Force
|
In the next articles of this series, we will show you how you can create an Azure VM via the Azure Resource Manager (ARM).
No comments:
Post a Comment