Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleUsing Azure Powershell
Note
title

Before proceeding please ensure you have installed Azure Powershell. For more information, see Install Azure Powershell. In addition, please ensure you run the Powershell application with elevated privileges.

  1. Login to your Azure subscription

    Connect to your Azure account and ensure you are connected to the correct subscription.

    Code Block
    languagepowershell
    titlePowerShell
    # To log in to Azure Resource Manager
    Login-AzureRmAccount
    
    # You can use a TenantID swtich for faster log in
    #Login-AzureRmAccount -TenantID xxxxxxxxxx
    
    # To view all subscriptions tied to your account
    Get-AzureRmSubscription
    
    # To switch subscriptions
    Get-AzureRmSubscription -SubscriptionName "{SubscriptionName}" | Select-AzureRmSubscription
    
    
  2. Create a Resource Group
    If a Resource Group does not exist or you wish to use a new group, using the following script to create one:

    Code Block
    languagepowershell
    titlePowerShell
    #Create a Resource Group
    New-AzureRmResourceGroup -Name "{ResourceGroupName}" -Location "{Location}"
    
    
  3. Create Availability Set(s) (Optional)

    If you are creating more than one virtual machine (or more correctly, deploying an application with more than one tier) it may be advantageous to create availability sets.  The purpose of availability sets is that the Azure platform has no way of distinguishing the application tiers associated with each VM.  This could lead to a single point of failure across your entire application.  When a VM is added to an availability set, by default Azure assigns it to two Fault Domains and five Update Domains.  The VMs are allocated across these domains to ensure that not all VMs within a set will fail together.  It is important to note that availability sets that contain only a single VM are not subjects to Azure's SLA.  More information on availability sets can be found here.

    Code Block
    languagepowershell
    titlePowerShell
    # Ensure you have AzureRm.Compute module is installed
    # To view installed modules 
    Get-Module
    
    # To list all modules
    #Get-Module -ListAvailable
    
    # If AzureRmCompute is not installed, go ahead and install it
    #Install-Module AzureRM.Compute
    
    # Create an availability set
    New-AzureRmAvailabilitySet -ResourceGroupName "{ResourceGroupName}" -Name "{AvailabilitySetName}" -Location "{Location}"
    # If you are using managed disks for your VM use the managed switch
    #New-AzureRmAvailabilitySet -ResourceGroupName "{ResourceGroupName}" -Name "{AvailabilitySetName}" -Location "{Location}" -managed
    
    
  4. Create Network Resources

    Unlike in Azure Portal where network resources are created during the VM setup, in Powershell, they must be explicitly instantiated beforehand and then assigned to the VM you are creating

    Code Block
    languagepowershell
    titlePowerShell
    # Create a subnet configuration (Note: you can create more than one subnet in your VN)
    $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name "{SubnetName}" -AddressPrefix "{IPAddressRange}"
    
    # Create a virtual network (multiple subnets can be added using a comma-delimited list)
    $vnet = New-AzureRmVirtualNetwork -ResourceGroupName "{ResourceGroupName}" -Location "{Location}" -Name "{VNName}" -AddressPrefix "{IPAddressRange}" -Subnet $subnetConfig
    # Create a public IP address and specify a DNS name (you may concatenate the $(Get-Random) function to the DNS Name for uniqueness
    $pip = New-AzureRmPublicIpAddress -ResourceGroupName "{ResourceGroupName}" -Location "{Location}" -AllocationMethod "{Static/Dynamic}" -IdleTimeoutInMinutes "{N}" -Name "{DNSName}"
    
    # Create any number of security rules
    # The example below allows inbound access on port 3389 for RDP connections
    $nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name "{RuleName}" -Protocol TCP `-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow
    
    # Create a network security group and assign the rules (multiple rules can be added using a comma-delimited list)
    $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName "{ResourceGroupName}" -Location "{Location}" -Name "{NetworkSecurityGroupName}" -SecurityRules $nsgRuleRDP
    
    # Create a network interface card and associate with subnet (in this case we are assigning the first subnet), IP address and security group
    $nic = New-AzureRmNetworkInterface -Name "{NicName}" -ResourceGroupName "{ResourceGroupName}" -Location "Location" -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id



  5. Create the Virtual Machine(s)

    Code Block
    languagepowershell
    titlePowerShell
    # Define a credential object
    $cred = Get-Credential
    
    
    # Create a virtual machine configuration
    $vmConfig = New-AzureRmVMConfig -VMName "{VMName}" -VMSize "{VMSize}" | `
    Set-AzureRmVMOperatingSystem "{OperatingSystem}" -ComputerName "{VMName}" -Credential $cred | `
    Set-AzureRmVMSourceImage -PublisherName "{PublisherName}" -Offer "{Offer}" -Skus "{Sku}" -Version latest | `
    Add-AzureRmVMNetworkInterface -Id $nic.Id






...