26 min read

(For more resources related to this topic, see here.)

Platform with PowerShell

Cloud is one of the most popular words in the ICT industry nowadays; we hear it every day, everywhere, and at every occasion. In simple words, cloud computing is the concept of using and delivering computing resources to the end user as a service. A computing resource could be software such as web portals and messaging systems, or hardware such as CPU, memory, network, and storage. It could be hosted internally in a corporate’s data center “Private Cloud” or externally in a vendor’s data center “Public Cloud”.

There are different types of cloud computing:

  • Software-as-a-Service (SaaS): In SaaS, you get your software (for example, e-mail, web portals, or CRM) as a service hosted in the cloud; you do not have to worry about hardware requirements, software prerequisites, implementation, and maintenance hassles. In simple words, you can get software that is ready to use in a few clicks.

    Popular solution(s): Microsoft Office 365 and Oracle CRM On Demand

  • Platform-as-a-Service (PaaS): In PaaS, you get Data Platform such as SQL Server as a service such as SaaS hosted in the cloud. You can also get your SQL Server instance and database ready in no time.

    Popular solution(s): Microsoft SQL Azure and Google App Engine

  • Infrastructure-as-a-Service (IaaS): In IaaS, you get the infrastructure (hardware) components as a service. It is similar to the web hosting concept in which you get a specific hardware configuration to host your website; however, in IaaS you get the hardware configuration to build and host your virtual servers.

    Popular solution(s): Microsoft SQL Azure and Amazon Web Services

This article will cover how Windows PowerShell helps in administering, managing, and automating a cloud computing platform such as Microsoft Windows Azure.

We will also cover the following topics:

  • What is Windows Azure?
  • What is Windows Azure PowerShell?
  • Managing Windows Azure using PowerShell.

What Windows Azure is

Windows Azure is a cloud-computing concept that is created by Microsoft. Mainly, Windows Azure provides IaaS and PaaS, so you can think about using it in many scenarios, such as hosting a web application, deploying a centralized data store, building a development and testing environment, or even implementing a disaster recovery (DR) site for your on-premise environment.

What Windows Azure PowerShell is

Windows Azure comes with a very neat and easy, web-based management interface that allows you to do any task in a few clicks, but unfortunately this interface is a bit limited. For example, you cannot create a couple of virtual machines in one shot; you have to repeat the same steps twice in order to get two virtual machines. The same goes for the rest of the Azure tasks. That is why, Windows Azure provides a powerful scripting environment via Windows PowerShell to make it easier for administrators to automate multiple Azure tasks, such as the provisioning of virtual machines, application deployment, and infrastructure management.

Installing Windows Azure PowerShell

Windows Azure PowerShell is provided with the Windows PowerShell module as part of the Windows Azure Software Development Kit (SDK). In order to install Windows Azure PowerShell:

  1. Go to Windows Azure’s download page:

    http://www.windowsazure.com/en-us/manage/downloads/

  2. In the Windows section, click on Install to download the web installer EXE file for Windows Azure PowerShell:

  3. Launch the web installer file; click on Install to start Windows Azure PowerShell’s installation and configuration.
  4. Follow the installation wizard to complete the process.

After installing Windows Azure PowerShell, a module called “Azure” should be available in your system.

Making Windows PowerShell understand Windows Azure cmdlets

In order to use Windows Azure cmdlets in Windows PowerShell, you can either directly launch the Windows Azure PowerShell shortcut to quickly jump into the Windows Azure PowerShell environment or launch the import Windows Azure PowerShell module into your Windows PowerShell session using the following command:

#Import Windows Azure PowerShell module PS> Import-Module Azure

Connecting to your Windows Azure environment

After downloading, installing, and importing the Windows Azure PowerShell, you are just one step away from managing your Windows Azure environment using PowerShell. The last step is connecting to your Windows Azure subscription.

In order to set up your Windows Azure subscription in your PowerShell, you have to import the PublishSettings file that contains your Windows Azure subscription’s unique information, such as the subscription ID, name, service endpoint URL, and certificate thumbprint. This information will be used by PowerShell to reach your Windows Azure environment.

You can get the PublishSettings file easily by using the Get-AzurePublishSettingsFile cmdlet. This cmdlet will take you to the Windows Azure portal. When you enter your credentials, you will be redirected to an instructional page to generate and download your Windows Azure PublishSettings file for your subscription.

#Generate and download the Windows Azure PublishSettings File PS> Get-AzurePublishSettingsFile

Now you should have the publishsettings file called <AzurePublishSettings>.publishSettings that contains your Windows Azure subscription. The next step is to import it to PowerShell in order to define your subscription information into Windows PowerShell. To import the PublishSettings file, use the Import-AzurePublishSettingsFile cmdlet as follows:

#Import Windows Azure PublishSettings File PS> Import-AzurePublishSettingsFile <FileName>.publishsettings

Once the PublishSettings file is imported successfully, Windows PowerShell will set your subscription as a default subscription; so every time you open Windows PowerShell and use Windows Azure cmdlets, it will automatically connect to Windows Azure using the default subscription. In order to show your subscription information, use the Get-AzureSubscription cmdlet.

So at this point, we can say congratulations!! Your Windows PowerShell environment is now ready to manage your Windows Azure.

Getting started with Windows Azure scripting

In this section, we will help you get started with Windows Azure scripting and automation using a set of Windows PowerShell scenarios and examples.

Scenario 1 – creating a new Azure Affinity Group

In this scenario, you are a Windows Azure administrator who wants to get the best performance by making sure that any related cloud components associated with a specific cloud service are placed in the same data center, especially when Microsoft has multiple Azure data centers distributed across the United States, Europe, and Asia. So for this purpose, you need to create an “affinity group” in order to group the related components logically. For this, you will use the New-AzureAffinityGroup cmdlet with the following group of parameters:

  • -Name: Defines the name of the new affinity group
  • -Location: Defines the location of the affinity group; this will define which data center needs to be used to place the cloud components associated with this affinity group

Use the Get-AzureLocation cmdlet to get the list of available locations.

You can use the following code:

#Create New Azure Affinity Group PS> New-AzureAffinityGroup –Name "ContosoAffinityGroup" –Location
"West US"

Scenario 2 – creating a new Azure storage account

In this scenario, you will create an Azure storage account in order to allow your services, applications, and infrastructure to utilize Windows Azure storage. For this purpose, you will use the New-AzureStorageAccount cmdlet with the following group of parameters:

  • -StorageAccountName: Defines the name of the new azure storage account
  • -AffinityGroup: Defines the name of the affinity group that the storage account should be associated with

You can use the following code:

#Create New Azure Storage Account PS> New-AzureStorageAccount -StorageAccountName "contoso" -AffinityGroup
"ContosoAffinityGroup"

Scenario 3 – assigning a storage account to an Azure subscription

In this scenario, you will assign a previously created Azure storage account to your Windows Azure subscription in order to make sure that any task created under this subscription will use this storage account by default. For this purpose, you will use the Set-AzureSubscription cmdlet to define the Windows Azure subscription’s settings with the following group of parameters:

  • -SubscriptionName: Defines the name of the Azure subscription; use the Get-AzureSubscription cmdlet to list all the available subscriptions in Windows PowerShell
  • -CurrentStorageAccount: Defines the name of the previously created Azure storage account

You can use the following code:

#Assign Azure storage account to a specific azure subscription PS> Set-AzureSubscription -SubscriptionName <Subscription_Name>
-CurrentStorageAccount "Contoso"

Scenario 4 – creating a new Azure Cloud Service

In this scenario, you will create a Windows Azure Cloud Service. A cloud service describes the components of each solution you have on Windows Azure. For example, if you have a web application hosted on a web server (IIS) that connects to a database hosted on a database server (SQL Server), these two components should be called “Cloud Service”. So, cloud service is an essential requirement for any Azure component you want to create on Windows Azure. For this purpose, you will use the New-AzureService cmdlet with the following group of parameters:

  • -ServiceName: Defines the name of the new Azure service
  • -AffinityGroup: Defines the affinity group the storage account should be associated with

You can use the following code:

#Create new Azure Cloud Service PS> New-AzureService -ServiceName "myCloudService" -AffinityGroup
"ContosoAffinityGroup"

Scenario 5 – creating a new SQL Azure Database Server

In this scenario, you will create a centralized SQL Server database hosted in the cloud, utilizing the SQL Azure capabilities to host your database. So before you create your SQL Server database, you first need to create a SQL Server instance to host this SQL database. For this purpose, you will use the New-AzureSqlDatabaseServer cmdlet with the following group of parameters:

  • -AdministratorLogin: Defines the login name for the SQL Azure instance administrator
  • -AdministratorLoginPassword: Defines the login and password for the SQL Azure instance administrator
  • -Location: Defines the SQL Azure server instance’s location

You can use the following code:

#Create new SQL Azure Database Server instance PS> New-AzureSqlDatabaseServer -AdministratorLogin "SherifT"
-AdministratorLoginPassword "P@ssw0rd" -Location "West US"

Scenario 6 – creating a new SQL Azure database

In this scenario, you will create a SQL Azure database on a previously created SQL Azure database server instance. For this purpose you will use a couple of cmdlets; the first cmdlet is the New-AzureSqlDatabaseServerContext cmdlet that is used to define which server you will connect to, and the second cmdlet is the New-AzureSqlDatabase cmdlet that is used to create the SQL Azure database.

Following are the group of parameters:

  • New-AzureSqlDatabaseServerContext
    • -ServerName: Defines SQL Azure server name
  • New-AzureSqlDatabase
    • -Context: Defines the database context object created using the New-AzureSqlDatabaseServerContext cmdlet
    • -DatabaseName: Defines the name of the new database you want to create
    • -Collation: Defines the collation of the database
    • -Edition: Defines the database edition, either “Web” or “Business”
    • -MaxSizeGB: Defines the maximum size of the database in gigabytes; the maximum size of the database depends on which database edition you are using

You can use the following code:

#Create SQL Azure Database Server Context PS> $context = New-AzureSqlDatabaseServerContext -ServerName <server_Name> #Create new SQL Azure Database PS> New-AzureSqlDatabase –Context $context -DatabaseName "myDatabase"
–Collation SQL_Latin1_General_CP1_CI_AS -Edition "Web" -MaxSizeGB 1

Scenario 7 – creating a new SQL Azure Database Server firewall rule

In this scenario, you will create a SQL Azure Database server firewall rule in order to allow communication between your SQL Azure database server and web application that is hosted either somewhere else or on other computers in a specific network range. For this purpose, you will use the New-AzureSqlDatabaseServerFirewallRule cmdlet to define the firewall rule settings for SQL Azure Server.

Following are the group of parameters:

  • -ServerName: Defines SQL Azure server name
  • -RuleName: Defines the name of the firewall rule
  • -StartIpAddress: Defines the start IP address
  • -EndIpAddress: Defines the end IP address

Use 0.0.0.0 for -StartIpAddress and -EndIpAddress to allow communication between SQL Azure and Windows Azure.

You can use the following code:

#Create SQL Azure Database Server Firewall Rule PS> New-AzureSqlDatabaseServerFirewallRule –ServerName <Server_Name>
-RuleName "myIntranet" -StartIpAddress 192.168.1.1 -EndIpAddress
192.168.1.254

Scenario 8 – provisioning the new Azure VM in Windows (quick mode)

In this scenario, you will create a new Windows Azure virtual machine running a Windows operating system using the quick mode. The quick mode allows you to create a new virtual machine with minimal input from your side; it is good for testing purposes. For the purpose of creating a new virtual machine, you will use the New-AzureQuickVM cmdlet with the following group of parameters:

  • -Windows: Defines that a virtual machine will run a Windows operating system.
  • -ServiceName: Defines the cloud service that will host the virtual machine.
  • -Name: Defines the name of the virtual machine.
  • -Password: Defines the operating system’s administrator password.
  • -ImageName: Defines the name of the image that will be used to provision the virtual machine.

    Use the Get-AzureVMImage cmdlet to list all the images available in Windows Azure.

  • -InstanceSize: Defines the size of the virtual machine: “ExtraSmall”, “Small”, “Medium”, “Large”, or “ExtraLarge”. The difference between instance sizes is the number of CPU cores and memory.

You can use the following code:

#Create new Windows Azure VM – Windows using Quick Mode PS > New-AzureQuickVM –Windows -ServiceName "DatabaseService" -Name
"CAI-DC-03" -ImageName "MSFT__Windows-Server-2012-Datacenter-
201210.01-en.us-30GB.vhd" -Password P@ssw0rd -AffinityGroup
"ContosoAffinityGroup" -AffinityGroup "ContosoAffinityGroup"

Scenario 9 – provisioning the new Azure VM in Linux (quick mode)

In this scenario, you will create a new Windows Azure virtual machine that is running Linux operating system using the quick mode. For the purpose of creating a new virtual machine, you will use the New-AzureQuickVM cmdlet with the following group of parameters:

  • -Linux: Defines that the virtual machine will run a Linux operating system.
  • -ServiceName: Defines the cloud service that will host the virtual machine.
  • -Name: Defines the name of the virtual machine.
  • -LinuxUser: Defines the Linux administrator user.
  • -Password: Defines the operating system’s administrator password.
  • -ImageName: Defines the name of the image that will be used to provision the virtual machine. Use the Get-AzureVMImage cmdlet to list all the available images.
  • -InstanceSize: Defines the size of the virtual machine: “ExtraSmall”, “Small”, “Medium”, “Large”, or “ExtraLarge”. The difference between instance sizes is the number of CPU cores and memory.

You can use the following code:

#Create new Windows Azure VM – Linux using Quick Mode PS> New-AzureQuickVM -Linux –ServiceName "myLinuxEnv" -Name "SUSE-02"
–ImageName "b4590d9e3ed742e4a1d46e5424aa335e__SUSE-Linux-Enterprise-
Server-11-SP2-New" -LinuxUser "root" -Password P@ssw0rd -AffinityGroup
"CoontosoAffinityGroup"

Scenario 10 – provisioning the new Windows Azure VM (advanced mode)

In this scenario, you will create a Windows Azure virtual machine that is running the Windows operating system using the advanced mode in order to add extra configurations that are not available using the quick mode, such as virtual machine disk and endpoint configurations. For this purpose, you will use a combination of the following Azure cmdlets:

  • The New-AzureVMConfig cmdlet is used to configure a new virtual machine configuration. Its parameters are as follows:
    • -Name: Defines the name of the virtual machine.
    • -ImageName: Defines the name of the image that will be used to provision the virtual machine.
    • -InstanceSize: Defines the size of the virtual machine: “ExtraSmall”, “Small”, “Medium”, “Large”, or “ExtraLarge”. The difference between instance sizes is the number of CPU cores and memory.
  • The Add-AzureProvisionConfig cmdlet is used to define the virtual machine’s provision configuration, such as the operating system, domain, time zone, and automatic updates. Its parameters are as follows:
    • -Windows: Defines that the virtual machine will run a Windows operating system
    • -Password: Defines the operating system’s administrator password
    • -DisableAutomaticUpdates: Disables the automatic update feature on the virtual machine
    • -ResetPasswordOnFirstLogon: Forces the user to change the password on the first login
    • -TimeZone: Defines the time zone for the virtual machine
    • -WindowsDomain: Defines that the virtual machine will join a domain
    • -Domain: Defines the name of the domain the virtual machine will join
    • -JoinDomain: Defines the fully qualified domain name (FQDN) of the domain the virtual machine will join
    • -DomainUserName: Defines the username of the domain account that has permission to join the virtual machine to the domain
    • -DomainPassword: Defines the password for the domain username
    • -MachineObjectOU: Defines the fully qualified domain name (FQDN) for the Organizational Unit (OU) in which the computer account will be created
  • The New-AzureVM cmdlet is used to create a new virtual machine using the previously created virtual machine configuration and provisioning configuration. Its parameters are as follows:
    • -ServiceName: Defines the cloud service that will host the virtual machine
    • -VMs: Defines the virtual machine configuration object that will be used to create the virtual machine

You can use the following code:

#Create Azure VM configuration PS> $vm1 = New-AzureVMConfig -Name myWeb01 -InstanceSize Medium -ImageName
"a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-
201212.01-en.us-30GB.vhd" | Add-AzureProvisioningConfig -Windows –Password
"P@ssw0rd" –WindowsDomain –Domain "Contoso" –JoinDomain "Contoso.com"
–DomainUserName "Administrator" –DomainPassword "P@ssw0rd"
-MachineObjectOU "OU=Azure,DC=Contoso,DC=com" -DisableAutomaticUpdates
–ResetPasswordOnFirstLogon –TimeZone "Pacific Standard Time" #Create Azure VM using the previously created VM PS> New-AzureVM -ServiceName "ContosoWeb" -VMs $vm1

Scenario 11 – Adding a new endpoint to Windows Azure VM (NoLB)

In this scenario, you have a secure website running on the Windows Azure virtual machine and you want to make this website accessible to other users. In order to achieve this task, you will have to create an endpoint that is configured to allow communication between users in different networks and the website on your virtual machine. Network Endpoint is similar to the concept of Network Address Translation (NAT) or the Port Forward features in network switches and routers.

To make your website accessible to other users, you will use the Add-AzureEndPoint cmdlet with the following parameters to add a new Not Load-Balanced (NoLB) endpoint to the secure website-utilizing port 443 for HTTPS:

  • -Name: Defines the name of the Endpoint rule
  • -Protocol: Defines the protocol of the endpoint, either TCP or UDP
  • -LocalPort: Defines the local port of the endpoint that is used for communicating with the application on the virtual machine
  • -PublicPort: Defines the public port that the Endpoint will use to listen to incoming requests

You can use the following steps:

  1. Get the Windows Azure VM you want to assign the Endpoint rule to, using the Get-AzureVM cmdlet with the –ServiceName and –Name parameters.
  2. Use the Add-AzureEndPoint cmdlet to add a new endpoint.
  3. Commit the changes on the virtual machine using the Update-AzureVM cmdlet.

You can use the following code:

#Add NoLB EndPoint to Windows Azure virtual machine PS> Get-AzureVM -ServiceName "CorpWebsite" -Name "WebSrv01"
| Add-AzureEndpoint
-Name "HTTPs" -Protocol tcp -LocalPort 443 -PublicPort 443
| Update-AzureVM

Scenario 12 – configuring the Windows Azure Virtual Machines load balancing (LB)

In this scenario, you have cloud services running on the corporate website portal; these cloud services consist of three virtual web servers, and you want to make sure that load balancing is configured for this server. In order to achieve this task, you will have to add a new Load-Balancing Endpoint and assign this endpoint to all those virtual machines that should be members of the load balancing stack.

For this purpose, you will use the Add-AzureEndPoint cmdlet with the following parameters to add a new Load-Balanced (LB) EndPoint for the server hosting the website portal:

  • -Name: Defines a name for the EndPoint rule
  • -Protocol: Defines the protocol for the endpoint, either TCP or UDP
  • -LocalPort: Defines the local port for the endpoint that is used for communicating with the application on the virtual machine
  • -PublicPort: Defines the public port that the endpoint will use to listen to incoming requests
  • -LBSetName: Defines a name for the Load-Balanced EndPoint set
  • -ProbeProtocol: Defines the protocol for the Load-Balanced EndPoint that is to be probed (tested); it’s either HTTP or TCP
  • -ProbePort: Defines the port to be used by the probes; by default, the public port is used if this parameter is not defined
  • -ProbePath: Defines the URI to be used by the probes; it’s used only with the HTTP probe protocol

You can use the following steps:

  1. Get all Windows Azure VMs under the cloud service that you want to assign a Load-Balanced EndPoint to, using the Get-AzureVM cmdlet with the -ServiceName parameter.
  2. Use the Add-AzureEndPoint cmdlet to add a new endpoint.
  3. Commit the changes on the virtual machine using the Update-AzureVM cmdlet.

You can use the following code:

#Add Load-Balanced EndPoint to Windows Azure virtual machine PS> Get-AzureVM -ServiceName CorpWebsite | Add-AzureEndpoint -Name
"LB-Http" -Protocol tcp -PublicPort 80 -LocalPort 80 -LBSetName
"LB-WebFarm" -ProbePort 80 -ProbeProtocol "http" -ProbePath "/"
| Update-AzureVM

Scenario 13 – creating and assigning a data disk to Windows Azure Virtual Machine

In this scenario, you have a Windows Azure virtual machine with only one disk for the operating system, and you want to create a new data disk and attach it to this virtual machine. For this purpose, you will use the Add-AzureDataDisk cmdlet with the following group of parameters:

  • -CreateNew: Creates a new data disk
  • -DiskLabel: Defines the disk label for the new data disk
  • -DiskSizeInGB: Defines the data disk size in gigabytes
  • -LUN: Defines the Logical Unit Number (LUN) location for the data disk in the virtual machine; you can assign LUN from 0 to 15

You can use the following steps:

  1. Get the Windows Azure VM to which you want to assign the data disk, using the Get-AzureVM cmdlet with the –ServiceName and –Name parameters.
  2. Use the Add-AzureDataDisk cmdlet with –CreateNew to create a new data disk.
  3. Commit the changes on the virtual machine using the Update-AzureVM cmdlet.

You can use the following code:

#Create and Assign a new data disk to Windows Azure VM PS> Get-AzureVM -ServiceName "myWebFarm" -Name WebSrv01
| Add-AzureDataDisk
-CreateNew -DiskSizeInGB 30 –DiskLabel "UserDataDisk" -LUN 0
| Update-AzureVM

Scenario 14 – moving the Local VHD to Windows Azure

In this scenario, you want to migrate a virtual machine from your on-premise Hyper-V server to Windows Azure without rebuilding the server from scratch. So you have decided to move the local VHD file for the virtual machine to your storage on Windows Azure. For this purpose, you will use the Add-AzureVhd cmdlet with the following group of parameters:

  • The Add-AzureVhd cmdlet is used to move the VHD file from the local server to Windows Azure. Its parameters are as follows:
    • -LocalFilePath: Assigns the file path for the local VHD file
    • -Destination: Assigns the URI for the Windows Azure container to which the VHD will upload
  • The Add-AzureDisk cmdlet is used to add the VHD to the Windows Azure Disk library. Its parameters are as follows:
    • -OS: Defines that the VHD is an operating system disk; it accepts either Windows or Linux
    • -DiskName: Defines the name of the disk on the library
    • -MediaLocation: Defines the location of the VHD that is to be added to the disk library

You can use the following steps:

  1. Define a variable $LocalVHD to store the local path for the VHD file.
  2. Define a variable $Destination to store the URI for the Windows Azure container.
  3. Use the Add-AzureVhd cmdlet to move the VHD file.
  4. Use the Add-AzureDisk cmdlet to convert the VHD to Azure Disk and store it in the disk library.

You can use the following code:

#Get the Azure Storage Account for the default Azure Subscription PS> $StorageAccountName = (Get-AzureSubscription).CurrentStorageAccount #Define DiskName PS> $DiskName = "AppVServerDisk" #Define Local VHD file path PS > $LocalVHD = 'D:Hyper-VVirtual Hard DisksAppVServer.vhd' #Define the URI for the Windows Azure Container PS > $Destination = 'http://' + $StorageAccountName + '.blob.core.windows.
net/vhds/AppVServerDisk.vhd' #Move VHD file from local server to Windows Azure Storage PS > Add-AzureVhd -LocalFilePath $LocalVHD -Destination $Destination #Convert the VHD file to Windows Azure Disk PS > Add-AzureDisk -OS Windows -DiskName $DiskName -MediaLocation
$Destination

Scenario 15 – provisioning a new Windows Azure VM from a Disk

In this scenario, you have a VHD for one of your virtual servers that has recently been moved from the on-premise Hyper-V server to the Windows Azure storage, and you want to create a new Windows Azure virtual machine using this VHD. For this purpose, you will use a combination of the following Azure cmdlets:

  • The New-AzureVMConfig cmdlet is used to create a new virtual machine configuration. Its parameters are as follows:
    • -Name: Defines the name for the new virtual machine.
    • -DiskName: Defines the name of the disk that will be attached to the virtual machine to provision it.
    • -InstanceSize: Defines the size of the virtual machine: “ExtraSmall”, “Small”, “Medium”, “Large”, or “ExtraLarge”. The difference between instance sizes is the number of CPU cores and memory.
  • The New-AzureVM cmdlet is used to create a new virtual machine using the previously created virtual machine configuration and provisioning configuration. Its parameters are as follows:
    • -ServiceName: Defines the cloud service that will host the virtual machine
    • -VMs: Defines the virtual machine configuration object that will be used to create the virtual machine

You can use the following code:

#Create Azure VM Configuration object PS> $vm1 = New-AzureVMConfig -Name AppVServer -InstanceSize Medium
-DiskName "AppVServerDisk" #Create new VM from Azure VM Configuration PS> New-AzureVM -ServiceName "ContosoWeb" -VMs $vm1

Scenario 16 – creating Windows Azure Image from a VM

In this scenario, you have a customized Windows Azure virtual machine and you want to use this virtual machine as a base image for the future provisioning of virtual machines. For this purpose, you will use the Save-AzureVMImage cmdlet to capture the virtual machine and save it as an image.

The parameters of the Save-AzureVMImage cmdlet are as follows:

  • -ServiceName: Defines the name of the cloud service hosting the virtual machine
  • -Name: Defines the name of the virtual machine
  • -NewImageName: Defines a name for the new image

Make sure to Sysprep your virtual machine before using the Save-AzureVMImage cmdlet.

You can use the following code:

#Create Azure VM Image PS> Save-AzureVMImage -ServiceName "CorpWebsite" -Name "myWeb01"
-NewImageName "Corp Website Core Image, Update Jan 2013"

Scenario 17 – exporting and importing Windows Azure VM

In this scenario, you have a Windows Azure virtual machine running under a specific cloud service and you want to move it to another cloud service. Unfortunately, there is no option in the Windows Azure portal that allows moving the virtual machine between different cloud services. The workaround is to use a combination of Windows Azure PowerShell cmdlets to achieve this goal. These cmdlets are as follows:

  • The Export-AzureVM cmdlet is used to export a virtual machine state (configuration) to an XML file. Its parameters are as follows:
    • -ServiceName: Defines the name of the cloud service hosting the virtual machine
    • -Name: Defines the name of the virtual machine
    • -Path: Defines the path in which to export the XML state file
  • The Remove-AzureVM cmdlet is used to remove the current virtual machine and lease the attached disk. Its parameters are as follows:
    • -ServiceName: Defines the name of the cloud service hosting the virtual machine
    • -Name: Defines the name of the virtual machine

    The Remove-AzureVM cmdlet removes the virtual machine but not the attached disk.

  • The Import-AzureVM cmdlet is used to import the virtual machine state file. Its parameter is as follows:
    • -Path: Defines the path of the XML state file

    The Import-AzureVM cmdlet might import the virtual machine with a new IP Address.

  • he New-AzureVM cmdlet is used to create a new virtual machine using a state (configuration) XML file imported in the last step. Its parameter is as follows:
    • -ServiceName: Defines the name of the cloud service hosting the virtual machine

You can use the following code:

#Export Azure VM configuration PS> Export-AzureVM -ServiceName CorpWebsite -Name myWeb01 -Path
$homedesktopmyWeb01.xml #Remove Azure VM PS> Remove-AzureVM -ServiceName CorpWebsite -Name myWeb01 #Importing Azure VM configuration file, and create new VM using the import
file PS> Import-AzureVM -Path $homedesktopmyWeb01.xml | New-AzureVM -
ServiceName CorpPortal

Scenario 18 – starting, stopping, and restarting the Windows Azure VM

In this scenario, you have a large number of Windows Azure virtual machines and you spend a lot of time starting, restarting, or stopping these using the management portal. Using the management portal, you are doing this task one by one and you want to discover the other possibilities in PowerShell. For this purpose, Windows Azure PowerShell provides a quick and basic task equivalent to this:

  • The Start-AzureVM cmdlet used to power on a virtual machine
  • The Stop-AzureVM cmdlet used to shut down a running virtual machine
  • The Restart-AzureVM cmdlet used to restart a virtual machine

All three cmdlets use the same parameters as follows:

  • -ServiceName: Defines the name of the cloud service hosting the virtual machine
  • -Name: Defines the name of the virtual machine

You can use the following code:

#Start Azure VM PS> Start-AzureVM -ServiceName CorpWebsite -Name myWeb01 #Restart Azure VM PS> Restart-AzureVM -ServiceName CorpWebsite -Name myWeb01 #Shutdown Azure VM PS> Stop-AzureVM -ServiceName CorpWebsite -Name myWeb01

Scenario 19 – uploading the certificate to Windows Azure

In this scenario, you have a Secure Socket Layer (SSL) certificate that you want to use for one of the services hosted on Windows Azure. In order to use it, you will have to upload it first to your Windows Azure subscription. For this purpose, you will use the Add-AzureCertificate cmdlet with the following group of parameters:

  • -ServiceName: Defines the cloud service in which you will deploy the certificate
  • -CertToDeploy: Defines the local path for the certificate files such as CER and PFX certificates
  • -Password: Defines the certificate password, if any

You can use the following code:

#Upload certificate to Windows Azure service PS> Add-AzureCertificate -ServiceName "myDevEnv" –CertToDeploy
<myCertificate.pfx> -Password abc123

Scenario 20 – generating the Azure Virtual Machine RDP file

In this scenario, you will generate a remote desktop file for your Windows Azure virtual machines so you can connect to them directly instead of using the Windows Azure portal. For this purpose, you will use the Get-AzureRemoteDesktopFile cmdlet with the following group of parameters:

  • -ServiceName: Defines the cloud service in which your virtual machine resides
  • -Name: Defines the name of the virtual machine for which you want to generate the RDP file
  • -LocalPath: Defines the local path where you want to save the RDP file
  • -Launch: Launches the remote desktop session for the selected session

You can use the following code:

#Generate Remote Desktop File for Windows Azure VM PS> Get-AzureRemoteDesktopFile -ServiceName "myDevEnv" –Name
"DevTools" -LocalPath $homeDesktopDevTools.rdp –Launch

Summary

It is very obvious that cloud computing is the future of our ICT industry; it is going to be a core component in each and every entity, and this is no secret. There are huge benefits and roadmaps provided by this technology for future growth.

In this article, we have seen Microsoft Windows Azure and SQL Azure as a real-life example of a cloud computing implementation, and we have learned how Windows PowerShell can play a major role in operating such a technology easily, as if managing a normal virtualized environment.

In the next article, we will talk about IT Process Automation (also known as Runbook automation) and the concept behind it. Also, we will learn how Windows PowerShell and System Center Orchestrator can be integrated together to implement and complete this concept in real life.

Resources for Article :


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here