Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Do anything you want!


Overview

This tutorial describes the use of Windows PowerShell on Azure cloud.

Powershell refers to both the command-line shell and scripting language designed system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows. Windows PowerShell cmdlets, let you manage the computers from the command line.

One-time tasks

Windows PowerShellGet Module.

If you don’t want to install these from the Web Platform Installer (wpilauncher.exe) at https://www.microsoft.com/web/downloads/platform.aspx

Install for ASM Imperative Commands

Imperative commands (verbs such as to start or stop an app or machine) are used in ASM.

  1. http://azure.microsoft.com/en-us/downloads
  2. Click PowerShell to download WindowsAzurePowershellGet.3f.3f.3fnew.exe and invoke it to download more.
  3. Click Install.
  4. Accept the pre-requisite of Windows Azure Powershell.

  5. Click Add for Azure Cross-platform Command-line Tools to download WindowsAzureXPlatCLI.3f.3f.3fnew.exe aka Power Tools

    NOTE: “XPlat” means Cross-platform. It’s for ASM portal usage.

Install for ARM Declarative Templates

declarative syntax are defined in Resource Manager templates used by ARM to enable infrastructure configurations to be defined (much like Puppet).

Install PowerShell

Run from the Powershell Gallery the Workflow to Download All Gallery Modules:

  1. Click the “Deploy” button or:

    
    Install-Script -Name Download-AllGalleryModules
    
  2. Press Y to accept that the modules are from an untrusted source.

  3. Click the “Deploy” button. You should see Azure’s Custon Deployment bolt with Parameters:

  4. Type in for new Resource Group name “Download-AllGalleryModules”.
  5. Click Create to see error icons.
  6. Click Edit parameters.
  7. Select your Resource group location (such as “East US 2”).
  8. Click Review legal terms then click Purchase.
  9. Click Create.

Make Imperative Commands

PS C:\>

Windows PowerShell providers access data stores, such as the Windows Registry and certificate store, as easily as you access the file system. Install NuGet provider:


   Install-PackageProvider -Name NuGet -Force
   

Get a count of how many commands for Azure module:


   Get-Command -Module Azure | Measure-Object
   

I got a count of 697 commands for just Azure for ASM.

List Azure commands containing “vm”:


   Get-Command -Module Azure -noun *vm*
   

Enable PS1 execution

PowerShell commands can be be script files with .ps1 file extension.


   Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
   

What Version

As with all PowerShell versions:


   $PSVersionTable
   

On Windows 10:

Name                           Value                                                                                   
----                           -----                                                                                   
PSVersion                      5.0.10586.63                                                                            
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
BuildVersion                   10.0.10586.63                                                                           
CLRVersion                     4.0.30319.42000                                                                         
WSManStackVersion              3.0                                                                                     
PSRemotingProtocolVersion      2.3                                                                                     
SerializationVersion           1.1.0.1                                                                                 

Compare against the response on Windows 7:

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5420
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

On-going basic commands:

List Mangement Verbs


   azure
   

Clear Screen


   cls
   

Download help files:


   update-help -force
   

Pop-up help for a command to a different window for multiple windows:


   help Get-AzureSubscription -ShowWindow
   

Authenticate


   azure login
   

Copy the code and open https://aka.ms/devicelogin


   azure account list
   

   add-azureaccount
   

   Get-AzureSubscription
   

Envrionment variables

To list, remember the colon at the end:


   Get-ChildItem Env:
   

For the value to a specific variable:


   Get-ChildItem Env:PATHEXT
   

Define a temporary environment variable:


   $env:MyTestVariable = "A temporary test variable."
   

Define a new permanent environment variable:


   [Environment]::SetEnvironmentVariable("TestVariableName", "My Value", "option")
   

In option is either “Machine”, “User”, or “Process”.

For loops

Based on http://www.symbiosysconsulting.com/pinging-from-powershell


   1..254 | ForEach-Object { ping "192.168.0.$_" }
   

Notice “$_” is the placeholder variable for the range before the pipe.

This loops through a range of IP’s within an internal subnet to show which ones respond:


   (
    (1..254) | % {
        $ping = New-Object System.Net.NetworkInformation.Ping;
        [Void](Register-ObjectEvent $ping PingCompleted -Action {
            param($s, $e);
            if($e.Reply.Status -eq "Success") {
                Write-Host $e.Reply.Address, ($e.Reply.RoundtripTime.toString() + "ms")
            }
        })
        $ping.SendPingAsync("192.168.0.$_")
    }
   ).Wait()
   

Rather than looping:

Declarative Templates

Multiple services can be deployed at the same time (asychronously), as a group, along with their dependencies by using a group template that defines desired end state of application components.

The Local Configuration Manager (LCM) introduced in Windows PowerShell 5.0 is the engine of DSC = Desired State Configuration.

Differences in each stage of the application lifecycle can be specified.

This makes it easy to get a total bill by viewing the rolled-up costs for the entire group or for a group of resources sharing the same tag.

See Azure Resource Manager overview by Tom FitzMacken.

At http://github.com/Azure are
sample ARM JSON templates at azure-quickstart-template code presented here.

Every template contains this:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "conventVersion": "1.0.0.0",
    "parameters":{
      "storageAccountUniqueName": {
         "type": "String",
         "metadata": {
           "description": "Unique name of storage account"
       }
      "storageAccountType": {
         "type": "String",
         "defaultValue": "Standard_LRS",
         "allowedValues": [
              "Standard_LRS",
              "Standard_GRS",
              "Standard_RAGRS",
              "Premium_LRS",
          ]
       }
    }
  },
    "variables":{

  },
    "resources":{

  },
    "outputs":{

  }
}

Types

  • Standard_LRS = Locally Redundant Storage
  • Standard_GRS = Geographically Redundant Storage
  • Standard_RAGRS = Read Access Geographically Redundant Storage
  • Premium_LRS =

Override

Parameters can be overriden with separate parameter files references:

{
    "type": "Microsoft.Storage/storageAccounts",
    "name": "variables('StorageAccountName')",
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-05-01-preview",
    "propterties": {
      "accountType": "[parameters('storageAccountType')]"
    }
}

“[resourceGroup().location]” enables the resource group to span across regions.

Load Balancer

An example DNS host name is mydeployment.eastus.cloudapp.azure.com, IP 23.99.9.198.

Up to 100 vms can be supported by a Load Balancer.

NAT rules on the Load Balancer route inbound traffic dynamically or statically to reserved IPs.

Virtual Machine images

An example declarative template would include:

    "imageReference": {
      "publisher": "MicrosoftSQLServer",
      "offer": "SQL2014-WS2012R2",
      "sku": "Standard",
      "version": "latest"
    }

publisher options:

  • “MicrosoftSQLServer”
  • redhat
  • barracuda

sku options:

  • “Standard”
  • “Web”
  • “Enterprise”
  • “EnterpriseOptimized”
  • “EnterpriseOptimizedDW” for Data Warehouse needing fast read but can tolerate slower bulk writes
  • “EnterpriseOptimizedOLTP” needing fast read and fast write

“offer” options:

  • “SP2014SP1-WS2012R2”
  • “SQL2014-WS2012R2”
  • etc.

The equivalent

  • Get-AzureRmVMImagePublisher -Location $locName select PublisherName
  • Get-AzureRmVMImageOffer -Location $locName -PublisherName $Publisher
  • Get-AzureRmVMImageSku -Location $locName -PublisherName $Publisher -Offer $offer

More on DevOps

This is one of a series on DevOps:

  1. DevOps_2.0
  2. User Stories for DevOps

  3. Choices for DevOps Technologies
  4. Java DevOps Workflow
  5. AWS DevOps (CodeCommit, CodePipeline, CodeDeploy)
  6. AWS server deployment options

  7. Digital Ocean
  8. Cloud regions
  9. AWS Virtual Private Cloud
  10. Azure Cloud Powershell

  11. Git and GitHub vs File Archival
  12. Git Commands and Statuses
  13. Data Security GitHub
  14. Git Commit, Tag, Push
  15. Git Utilities
  16. GitHub API

  17. TFS vs. GitHub

  18. Jenkins Server Setup
  19. Jenkins Plug-ins
  20. Jenkins Freestyle jobs
  21. Jenkins2 Pipeline jobs using Groovy code in Jenkinsfile

  22. Dockerize apps
  23. Docker Setup
  24. Docker Build

  25. Maven on MacOSX

  26. Powershell Ecosystem
  27. Powershell on MacOS
  28. Powershell Desired System Configuration

  29. Ansible

  30. MySQL Setup

  31. SonarQube static code scan

  32. API Management Microsoft
  33. API Management Amazon

  34. Scenarios for load