For more on the 31 Days of Server in the Cloud Series – see here: http://mythoughtsonit.com/2013/01/31-days-of-servers-in-the-cloud-windows-azure-iaas-and-you/
Before you are able to execute these PowerShell commands against your Azure environment you need to configure your workstation. You can follow these instructions here: http://mythoughtsonit.com/2013/01/31-days-of-servers-in-the-cloud-setting-up-management/
If you need to setup an Azure subscription to work against you can get a free 90 trial here: http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200120070
Ok, so you have Azure and you have your workstation configured. You may need to allow PowerShell commands to run on your machine by typing “Set-ExecutionPolicy RemoteSigned” from an Administrator elevated PowerShell prompt. You can see what it is currently set at by typing “Get-ExecutionPolicy”
Provisioning a Virtual Machine using PowerShell
For understanding how to create your Azure VM with PowerShell you just need to step through entering the commands in the table below and follow the points in the description section.
||Lists your Azure subscriptions so that you can specify it in the Set-AzureSubscription command below. Record yours because we will use it later.
My subscription that I used for this was “Windows Azure MSDN – Visual Studio Ultimate”
||This command lists your Azure Storage accounts or spaces. Choose your storage account. If you don’t have one yet you will need to create one.
I have two spaces one in the Eastern US and one in the Western US that the web tool created for me when I built VMs with it. For this VM I will create it in the West so I use the storage account “portalvhdsktmvsxbnw6j9h”
||This is how you set both the subscription and the storage account of where the VM will be created.
Set-AzureSubscription “Windows Azure MSDN – Visual Studio Ultimate” -CurrentStorageAccount portalvhdsktmvsxbnw6j9h
Click on the thumbnail to see the actual PowerShell output from the first three commands where I setup my environment
||This command will list all of the images in azure they you can use to install a new VM. I will use the Windows Server 2012 image. MSFT__Windows-Server-2012-Datacenter-201210.01-en.us-30GB.vhd
||The Get-AzureVMImage command lists out all the Images you have in Azure.
||Will list all of your current Azure VMs
||This command creates the new virtual machine. Here is the full command:
New-AzureQuickVM -Windows -ServiceName “bjl2″ -Name “bjl2″ -ImageName “MSFT__Windows-Server-2012-Datacenter-201210.01-en.us-30GB.vhd” -Password P@ssw0rd –Location “West US”
||Removes the Virtual Machine from Azure. You need to specify both the Service Name and the VM name. It will prompt you if you leave one out.
In this command prompt you can see the steps in the VM creation and removal process
1. List the VMs in Azure
2. Create a new VM
3. List the VMs in Azure – now there are two…
4. Remove the new VM from Azure
5. List the VMs in Azure – we are back to 1 VM.
If you want to know more information about other commands you can execute against Azure then check out the list of Windows Azure Cmdlets below. For more details on scripting the above commands see the post on Automating Azure with PowerShell.