Windows Azure PowerShell for IaaS

    Introduction

    Back in June 2012, the Windows Azure PowerShell cmdlets were updated and some interesting functionalities for managing Windows Azure virtual machines were added to it. In this article, I'll talk about some of the new Windows Azure IaaS automation features.

    Configuring Windows Azure PowerShell
    The first thing to do is install Windows Azure PowerShell. The easiest way to download and import the settings file is to download the template from the official site. After the publication settings file is uploaded, it must be imported:
     Import-AzurePublishSettingsFile 'c:\temp\mysub.publishsettings'
    

    It is also possible to manually configure the publication profile without downloading the settings file:
    $subid = '[YOUR-SUBSCRIPTION-ID]'
    $cert = Get-Item Cert:\CurrentUser\My\YOURCERTTHUMBPRINT
    Set-AzureSubscription -SubscriptionName 'testsub1' -SubscriptionId $subid -Certificate $cert 

    Please note that after the subscription settings are completed, the publication profile is saved by default in the following place: C: \ Users \ user \ AppData \ Roaming \ Windows Azure Powershell
    This means that you do not need to run Set-AzureSubscription for each of the scenarios , since he already is. Windows Azure PowerShell supports several subscriptions, so here you can choose which subscription to work with, using the Select-AzureSubscription command.

    Storage Setup

    image
    Also, a new addition is –CurrentStorageAccount. This parameter allows you to specify which storage to use for your VM's in work from PowerShell. To install the repository, run the following command:
    Get-AzureStorageAccount  

    If you need to create a StorageAccount, use the following command:
    New-AzureStorageAccount -StorageAccountName 'myuniquelynamedstorage' -Location 'East US'


    Preparing to create a virtual machine from PowerShell

    image
    Before you start creating VM's in Windows Azure, you need to set some settings that will be required to work from PowerShell, namely, the location:
      $dclocation = '[YOUR-LOCATION]' 

    Set the name of the cloud service that will act as the container for VM'S:
    Test-AzureName -Service '[YOUR-CLOUD-SERVICE-NAME]'
    $cloudSvcName = '[YOUR-CLOUD-SERVICE-NAME]'

    Determine which platform will be used as the basis for VM's:
    Get-AzureVMImage | select ImageName
    $image = '[YOUR-SELECTED-IMAGE-NAME]'

    Now you can start creating VM's from PowerShell.

    Quickly create Windows VM's from PowerShell

    After all the initial settings are completed, to quickly create a virtual machine, you must use the following commands:
    $adminPassword = '[PASSWORD]'
    $vmname = 'mytestvm'
    New-AzureQuickVM -Windows -ServiceName $cloudSvcName -Name $vmname -ImageName $image -Password $adminPassword 
    


    Quickly create Linux VM's from PowerShell

    
    $linuxuser = '[CHOOSE-USERNAME]'
    $adminPassword = '[YOUR-PASSWORD]'
    $vmname = 'mytestvm1'
    New-AzureQuickVM -Linux -ServiceName $cloudSvcName -Name $vmname -ImageName $image -LinuxUser $linuxuser 
    


    Rebooting, starting, and stopping the Windows Azure virtual machine

    
    # Перезагрузка
    Restart-AzureVM -ServiceName $cloudSvcName -Name $vmname
    # Остановка 
    Stop-AzureVM -ServiceName $cloudSvcName -Name $vmname
    # Старт
    Start-AzureVM -ServiceName $cloudSvcName -Name $vmname
    


    Advanced Virtual Machine Commands

    New-AzureVMConfig allows you to create a virtual machine by configuring for yourself. If you are not happy with the quick creation of VM's or if you don’t like the number of settings this command provides, then this section is for you. You can add a data disk, configure endpoints (automatically added for SSH and RDP), and even change the behavior of the OS disk cache or data disk. All that is required is to call the New-AzureVMConfig command and then transfer them to VM's.

    Creating Windows Virtual Machines from PowerShell

    $vmname2 = 'mytestvm2'
    $vmname3 = 'mytestvm3'
    $vm2 = New-AzureVMConfig -Name $vmname2 -InstanceSize ExtraSmall -ImageName $image |
              Add-AzureProvisioningConfig -Windows -Password $adminPassword |
              Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk1' -LUN 0 |
              Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' `
                  -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' 
    $vm3 = New-AzureVMConfig -Name $vmname3 -InstanceSize ExtraSmall -ImageName $image |
           Add-AzureProvisioningConfig -Windows -Password $adminPassword  |
            Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk2' -LUN 0  |
            Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' `
                  -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' 
    New-AzureVM -ServiceName $cloudSvcName -VMs $vm2,$vm3


    Creating a Linux Virtual Machine from PowerShell

    $vmname2 = 'mytestvm2'
    $vmname3 = 'mytestvm3'
    $vm2 = New-AzureVMConfig -Name $vmname2 -InstanceSize ExtraSmall -ImageName $image |
           Add-AzureProvisioningConfig -Linux -LinuxUser $linuxUser -Password $adminPassword |
           Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk1' -LUN 0 |
            Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' `
                  -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' 
    $vm3 = New-AzureVMConfig -Name $vmname3 -InstanceSize ExtraSmall -ImageName $image |
             Add-AzureProvisioningConfig -Linux -LinuxUser $linuxUser -Password $adminPassword |
             Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk2' -LUN 0 |
             Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'web' `
                 -LBSetName 'lbweb' -ProbePort 80 -ProbeProtocol http -ProbePath '/' 
    New-AzureVM -ServiceName $cloudSvcName -VMs $vm2,$vm3


    Upgrading existing virtual machines

    Changing existing virtual machines requires obtaining the current settings using the Get-AzureVM command; after editing, the Update-AzureVM command is used to save the settings. Some of the changes will require a reboot of the virtual machine, for example, disk cache settings.
    
    $vmname = 'mytestvm1'
    Get-AzureVM -Name $vmname -ServiceName $cloudSvcName |
        Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'datadisk1' -LUN 0 |
        Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel 'translogs1' -LUN 1 |
        Add-AzureEndpoint -Protocol tcp -LocalPort 1433 -PublicPort 2000 -Name 'sql' |
        Update-AzureVM 
    


    Conclusion

    In this topic, I examined the basic commands for working with virtual machines from PowerShell, which will help beginners in managing virtual machines in Azure.

    Also popular now: