다음을 통해 공유


DevOps – Creating and Configuring Azure Virtual Machines using PowerShell – Part 1

DevOps is a evolution within an organization that is focused on people, process and the right tools to make application lifecycle faster and more predictable

We start by creating and configuring the virtual machine in Microsoft azure

Create a Azure VM

Install the windows azure powershell from the link and run the below powershell scripts

  • Downloads the .publishsettings file for a Windows Azure subscription**.**
Get-AzurePublishSettingsFile
  • Imports a publish-settings file with a certificate to connect to your Windows Azure account
Import-AzurePublishSettingsFile –PublishSettingsFile "C:\Users\Girish\Downloads\Azure Pass-6-20-2015-credentials.publishsettings"
  • The New-AzureStorageAccount cmdlet creates an account that provides access to Azure storage services. This account will be used while storing the VHD


      $location = "Southeast Asia"  
      New-AzureStorageAccount -StorageAccountName "girishgoudar" -Location $location  
  • Set-AzureSubscription Configures and stores common Windows Azure subscription settings.
Set-AzureSubscription -SubscriptionName 'Pay-As-You-Go' -CurrentStorageAccount 'girishgoudar'
  • The Get-AzureVMImage cmdlet returns information for all images in the Azure Repository. Use an image as per the requirement
Get-AzureVMImage | select ImageName
$image = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201506.01-en.us-127GB.vhd"
  • The New-AzureVMConfig cmdlet creates a new virtual machine configuration object. This object can then be used to perform a new deployment, as well as to add a new virtual machine to an existing deployment.
$vm1 = New-AzureVMConfig -Name "myvm" -InstanceSize "Small" -ImageName $image
  • The Add-AzureProvisioningConfig cmdlet adds configuration information to a virtual machine configuration used to create a new virtual machine
$vm1 | Add-AzureProvisioningConfig -Windows -AdminUserName "USERNAME" -Password "PASSWORD"
  • Adds a new data disk to a virtual machine object.
$vm1 | Add-AzureDataDisk -CreateNew -DiskLabel 'data' -DiskSizeInGB 100 -LUN 0
  • The Add-AzureEndpoint cmdlet adds a new input endpoint to a virtual machine.
$vm1 | Add-AzureEndpoint -Name 'web' -PublicPort 80 -LocalPort 80 -Protocol tcp
  • The New-AzureService cmdlet creates a new Microsoft Azure service in the current subscription.
New-AzureService -ServiceName "girishgoudar" -Location $location
  • The New-AzureVM cmdlet adds a new virtual machine to an existing Azure cloud service, or creates a new virtual machine and a new cloud service in the current subscription if either the Location or AffinityGroup parameter is specified.
$vm1 | New-AzureVM -ServiceName "girishgoudar" -Location $location

Configuring a Azure VM
The Azure team released new Azure VM extension called DSC extension. Using DSC extension, we can push DSC configuration to an Azure VM

  • The New-AzureStorageContext cmdlet creates an Azure Storage context. This is required to upload the configuration to a storage account that requires authorization
$Key = Get-AzureStoragekey -StorageAccountName "girishgoudar"
$Context = New-AzureStorageContext -StorageAccountName 'girishgoudar' -StorageAccountKey $Key.Primary

Before we can enact DSC configuration in an Azure VM, we need to publish that configuration to an Azure storage container. This can be done using the Publish-AzureVMDscConfiguration cmdlet.The below configuration script needs to be saved as a .PS1 file on the local system as IISAndASPNet.ps1.This script will install the IIS and ASP.NET 4.5 role on the machine

Configuration IISAndASPNet
{
   Import-DscResource -Module xWebAdministration
   Node localhost
    {
        # Install the IIS role
        WindowsFeature IIS
        {
              Ensure          = "Absent"
              Name            = "Web-Server"
        }
        # Install the ASP.NET 4.5 role
        WindowsFeature AspNet45
        {
              Ensure          = "Absent"
              Name           = "Web-Asp-Net45"
        }
    }
}
Publish-AzureVMDscConfiguration -ConfigurationPath "C:\IISAndASPNet.ps1" -StorageContext $Context -ContainerName demopowershellcontainer
  • For enabling and enacting the configuration, we can use the Set-AzureVMDscExtension cmdlet
$vm = Get-AzureVM
Set-AzureVMDscExtension -VM $vm -ConfigurationArchive IISAndASPNet.ps1.zip -ConfigurationName AzureDscDemo -Verbose -StorageContext
$Context -ContainerName demopowershellcontainer | Update-AzureVM

In the next part, we will see how to use visual studio online and release management for deploying an application to Azure VM.