Вы находитесь на странице: 1из 4

AzureCon Challenge.

Creating and Connecting to a VM in Code.


Overview
In this challenge you will learn the very basics of what drives cloud computing and underpins many of the other services on Azure Virtual Machines and
Storage. Storage is a highly resilient, very fast, network storage system on Azure. You can store anything there, its useful for backup and DR scenarios, as
well as application content, web and media. One of its primary uses is to store the underlying Virtual Hard Disks for your virtual machines whether these are
disks dedicated to a Virtual Machine or shared across virtual machines.
In this challenge you will first quickly create a VM using automation code. You will create your storage account to store your Virtual Hard Disk for your VM
and then you will create your VM. You will then connect to your VM using Remote Desktop and then shut it down.

1. Login to Azure
For this challenge, you have either elected to use your own subscription or have created a new Azure
subscription using the provided Azure Pass (or Free Trial). If you want to switch to use the provided Azure
Pass the promotion code is displayed on the My Account page on the http://challenge.azurecon.com web
site. If there is no promo code displayed, you will need to use the free trial http://azure.microsoft.com/pricing/free-trial.
Azure has TWO management portals - the classic portal (http://manage.windowsazure.com) and a new
portal that is in Preview at http://portal.azure.com. You will use the Preview portal in this challenge.
1.

Open a browser and go to http://portal.azure.com

2.

Enter your Microsoft Account email address and password for the Microsoft Account you
associated with your Azure Pass or your own subscription.

3.

You will now be in your Azure subscription (see opposite) and from here you can create and
manage Azure services.

2. Download the Azure PowerShell cmdlets


In this lab you will use the Azure PowerShell cmdlets to control Azure from your own machine this lab assumes you are running on a Windows Machine. If
you are NOT and want to follow this lab the easiest way is to provision a new Windows VM in Azure and use that machine instead. You can follow the
instructions in the first part of Lab 1 (creating a quick VM and connecting to the VM) to do
this.
1.

Open a browser and go to https://github.com/Azure/azure-powershell/releases


(You are going to install the Windows Standalone release which is a minimal
release and does not include all of the Azure SDK which you dont need for this
lab).

2.

Click the Windows Standalone link (for the latest release which might not be
0.9.8 as shown) and then follow the instructions to RUN the .msi file and complete
the installation.

3. Some simple but useful things first


Anything you can do via the Azure Portal, you can also do in code in PowerShell. You will
first create your Azure Storage account this is needed to store the virtual hard disk for your
VM.
1.

On your machine, click the WINDOW button and type PowerShell ISE.

2.

Select the PowerShell ISE application in the list and PowerShell ISE will launch.

3.

First click the View menu and choose Show Script Pane.

4.

At the command prompt window (bottom left) type:


Add-AzureAccount and press Enter

Page | 1

If you get any errors here, you might need to restart your machine so that PowerShell ISE can pick-up the powershell modules.
A login dialog will fire up, when it does, enter the same Microsoft Account credentials when you logged into your Azure subscription. You will
then see in the command prompt window, some identity details for your subscription.
5.

In the command window, cut/paste these two commands press Enter after each one.
import-module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\azure.psd1"
import-module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureResourceManager.psd1"

6.

Type:

Get-AzureSubscription
This will list all the subscriptions that your login has access to likely it will
be only one. If you have multiple subscriptions, you need to pick a
particular subscription that you are going to use for this challenge.
7.

Type:

Select-AzureSubscription SubscriptionName
<insert the name of the subscription you want to
use> e.g. Azure Pass
Now, operations you perform will be done against the selected
subscription, you dont have to supply that value.
You need to create a storage account for your virtual disks and a storage account
needs to be put in an Azure region but how do you know which region you can put your storage account in? Your subscription might not have access to
all the regions.
8.

Type:

AzureResourceManager\Get-AzureLocation
9.

Wow, thats a list of all types of resources in Azure and for each one what the possible locations available are for your subscription. Lets narrow
this down to just the locations for storage accounts:

10.

Type (or copy/paste):

AzureResourceManager\Get-AzureLocation | select name, locations | where {$_.Name -eq


"Microsoft.Storage/storageAccounts"}
11.

Thats better. Now you are going to start writing a script in the script window in
ISE not issuing the commands one at a time. You are becoming a
programmer..! In the Script window, paste in the same command from
above. Press the RUN button on the tool bar (or Debug ->Run/Continue).
Same result? Yes.

12.

Just before the Get-AzureLocation command on the same line (still in the
script window). Type $storagelocations =

13.

Press RUN again this time no output. Thats because you just caused the
output of the Get-AzureLocations command to be assigned to the $storagelocations variable. This now holds some data about your storage
locations.

14.

You can get this data by referencing the variable $storagelocations and its properties such as Name and Locations.

15.

Add these lines AFTER your first line of script.

$storagelocations.Name
$justlocations = $storagelocations.Locations
"Random Region=" + (Get-Random -input $justlocations)
16.

Press Run several times. You will see that each time a new region is selected.
You can also see how to output text. Pretty useful.

17.

Select by clicking the line number 4 (see A) just the Random Region line. In
the toolbar indicated by (B) this is the Run Selection button it will only
execute whatever is highlighted NOT the whole script. Very useful for
debugging.

18.

Press Run Selection button a few times only this command is executed.

Page | 2

19. So your little program is working. Change the last line to the following so that the region itself is stored in a variable.
$region = (Get-Random -input $justlocations)

4. Creating the resources for your Virtual Machine


There are TWO ways you can create your VM.
a.

You can write commands to create all the resources you need for the VM as you have been doing in PowerShell already.

b.

You can create a definition of the resources (called a template) and then just use PowerShell and a simple script to hand that
template off to Azure.

You will be doing (a). There is a separate challenge on Azure Resource Manager Templates that covers (b).
Ok, you can now start writing the code/script to create your Virtual Machine. However, a Virtual Machine is dependent on a number of other services to be
created. For example, a VM must be attached to a resource group (which you have to create first). A VM needs virtual disks and these have to be stored in a
storage account which YES you have to create first So BEFORE you can create your VM, you have to create all the dependent resources.
1.

After your last line of code ($region = (Get-Random input ) copy/paste these lines.

$rgname = "VMinCode"
$storagename = "<alias>vmdiskstore"
New-AzureResourceGroup -Name $rgname -location $region
AzureResourceManager\New-AzureStorageAccount StorageAccountName $storagename Location
$region -ResourceGroupName $rgname -Type Standard_LRS
2.

Replace <alias> on the second line above - including the angle brackets with your
alias. This will be the name of your storage account it CANNOT have any symbols
in its name (e.g. bsmithvmstore).

3.

Press RUN. After a few seconds you will see in the command window that your
storage account creation and resource group was successful.

4.

Switch back to the Azure Portal and click on Resource Groups. Click the
VMinCode group and you will see it contains a single resource your storage
account.

5.

Your VM needs a few other things as well it needs a network (which needs a
subnet) and it needs a Virtual Network Interface Card (a Nic) and it needs a public IP
address. So lets create all these. Copy/Paste the script below at the end of your
current script but DO NOT press run.
$vmDNSname = "<alias>-vmincode"
$subnetName = "Subnet-1"
$subnet = New-AzureVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix "10.0.64.0/24"
$vnet = New-AzureVirtualNetwork -Name "VNET" -ResourceGroupName $rgname -Location $region -AddressPrefix
"10.0.0.0/16" -Subnet $subnet
$subnet = Get-AzureVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$pip = New-AzurePublicIpAddress -ResourceGroupName $rgname -Name "vip1" -Location $region AllocationMethod Dynamic -DomainNameLabel $vmDNSname
$nic = New-AzureNetworkInterface -ResourceGroupName $rgname -Name "nic1" -Subnet $subnet -Location
$region -PublicIpAddress $pip -PrivateIpAddress "10.0.64.4"

6.

Change <alias> above with your alias e.g. $vmDNSname = bmith-vmincode

7.

Now you dont want to run the entire script, just the last section you pasted in.
Highlight that section (a) and press the RUN SELECTION button (b)

8.

Switch back to the portal and you should see all the resources you have created so far.

Page | 3

5. Creating Your VM
Now its time to create your VM. The basic approach is to use a variable to hold all the information needed for the VM creation and then pass that variable
to the VM creation command. This special variable is called a VMConfig object. For our VM we need to define things like the credentials to use for the
admin login, the size of the VM, what OS will be installed in the VM, the name of the operating system disk and so on.
1.

Copy/paste in these 5 lines of code. SELECT the 5 lines and press the RUN SELECTION button.

$publisher = "MicrosoftWindowsServer"
$offer = "WindowsServer"
$sku = "2012-R2-Datacenter"
$version = "latest"
$cred = Get-Credential
2.

You will get a dialog asking you for a username/password and whatever you enter gets securely passed into the $cred variable. This will be the
username/password for the virtual machine you are creating. Suggest you use <alias>admin e.g. bsmithadmin as the username and a password
you can remember write it down..!

3.

In the command window (at the bottom left), type $cred. This will display the value of the username but not the password.

4.

Back in your script window at the top, copy/paste these 6 lines of script:
$vmConfig = AzureResourceManager\New-AzureVMConfig -VMName $vmDNSname -VMSize "Standard_A1"
AzureResourceManager\Set-AzureVMOperatingSystem -VM $vmconfig -Windows -ComputerName "contoso-w1" Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vmConfig | AzureResourceManager\Set-AzureVMSourceImage -PublisherName $publisher -Offer $offer -Skus
$sku -Version $version
$vmConfig | AzureResourceManager\Set-AzureVMOSDisk -Name "$vmDNSname-w1" -VhdUri
"https://$storagename.blob.core.windows.net/vhds/$vmDNSname-w1-os.vhd" -Caching ReadWrite -CreateOption
fromImage
$vmConfig | AzureResourceManager\Add-AzureVMNetworkInterface -Id $nic.Id
AzureResourceManager\New-AzureVM -ResourceGroupName $rgname -Location $region -VM $vmConfig

9.

Highlight the 6 lines and press RUN SELECTION this will create your Virtual Machine.

10.

Switch to the Portal. Click on Virtual Machines NOT Virtual


Machines (classic). If you want to understand the differences
between these two types of Virtual Machines complete the
challenge Your First Virtual Machine on Azure.

11.

You will see your VM creating..!

12.

Back in Powershell after several minutes, you will get some


information about the operation once it has completed.

13.

Optional: Click on your Virtual Machine in the Portal when the


status = RUNNING. Click the Connect button and follow the
prompts to connect to and login to your new VM.

14.

Shutdown your VM from the VM blade click STOP.

--- END OF LAB --Go back to the AzureCon Challenge web site (http://challenge.azurecon.com) and complete the challenge question to get your points.
REMEMBER: You only have one chance at the question, make sure you really know the answer!

Page | 4

Вам также может понравиться