Prashanth Jayaram
List VM Image offerings using AZ cmdlets and parameters

Deep dive into IT Cloud Automation using PowerShell

August 28, 2020 by
  • The future of PowerShell is bright—the scale in which Cloud automation adoption with major tech giants such as Microsoft, Google, Amazon, and VMWare is seemingly much higher.

    Cloud operations and Cloud automation using PowerShell redefined the scope of operations and automation responsibilities of cloud engineers and cloud administrators. As PowerShell continues to develop and evolve in the open-source world—there is no doubt that has a greater shift in the automation technology in terms of adoption and usability.

    In this article, we will be acquainted with Azure PowerShell and understand how it is connected to Microsoft Azure. First, we will see how PowerShell becomes a one-stop automation solution for multiple vendors. Lastly, we will see how to get the Azure VM image; how we can manage Microsoft Azure using PowerShell Azure (Az) and Az CLI commands.

    Microsoft cloud platform is a highly scalable workflow execution environment — Azure Automation provides a platform to allow you to orchestrate the deployment and manage the repeated tasks using run-books based on Windows PowerShell Workflow functionality. The run-books can significantly minimize human errors when carrying out repeated tasks and simplify the automation process.

    The adoption of PowerShell

    PowerShell became a first-class citizen in the recent past. Due to its simple constructs and ease of adopting the language, it becomes a complete cloud automation platform for the Azure Cloud platform. PowerShell is an automation and configuration framework provided by Microsoft. It comprises a command-line shell and a scripting language built on Microsoft .NET Framework.

    VMWare

    PowerShell is used in Microsoft and non-Microsoft products. For example, VMware—one of Microsoft’s biggest competitors—uses PowerShell to automate and manage VMware vSphere through the PowerShell management interface known as PowerCLI.

    Docker

    The community developed PowerShell module is in use to manage Docker containers.

    AWS Cloud

    PowerShell support for AWS using AWS Tools provides a platform for many administrators and developers to manage the AWS services and resources. It has become one of the most powerful toolsets one should have it in their toolset to manage Windows, Linux, and macOS environments

    Google Cloud

    Google Cloud also has Cloud Tools to support PowerShell. It is no different for you to script, manage, and automate the Windows workloads running on the GCP platform. The main intent of every provider is to provide an interface to underlying technology because we’re already more familiar with PowerShell, it is easy for us to automate, manage, administrator and customize Cloud platforms

    Azure Cloud

    Azure PowerShell is a set of cmdlets for managing Azure resources directly from the PowerShell command line. Azure PowerShell is designed to make it easy to learn and get started with the features for Cloud automation. Azure PowerShell works with PowerShell 5.1 on Windows, and PowerShell 6.x and higher on all platforms.

    The biggest difference between Azure PowerShell and Azure CLI is the binary (that can run on different platforms), and PowerShell Core is a shell that works across platforms. Azure Powershell is a bunch of PowerShell modules—everything else is derived from the core modules.

    • Note: You can use Azure CLI commands inside PowerShell scripts, but not vice-versa.

    Azure PowerShell is a module that provides a rich set of cmdlets to manage Cloud automation and Azure services. You can run or execute the cmdlets to create, test, deploy, integrate, and manage solutions and services offered in the Azure market platform. In all cases, it is easy to use the cmdlets to perform the same tasks that you can perform through the Azure Management Portal. For example, you can create and configure cloud services, virtual machines, virtual networks, configure the database, deploy automation, integrate third-party tools, and web apps.

    • Note: Azure PowerShell and Azure CLI both have the older versions—you need to know the parameters, and many of them are already deprecated. It is advised that you give the utmost importance when you’re working with the corresponding Azure toolsets. I will also recommend reading online resources and Microsoft documentation to understand better about the usage and functionality of each cmdlet.

    • Note: Microsoft promotes Powershell as Azure Cloud Automation Framework along with ARM (Azure Resource Manager) template, python, and Azure CLI.

    Get Started

    Microsoft Azure offers a variety of different services to store and retrieve data in the cloud automation framework.

    Let us see an example to list all the Windows images using PowerShell Azure(Az) and Az CLI tools. The cmdlet Get-AzVMImage cmdlets are part of the Az.Compute module. The output of Get-AZVMImage can also be derived using az cli—az vm image list command.

    In this section, you can see the methods to run the Az and Az CLI command to get VM image details.

    To list all the VM image versions using PowerShell Azure cmdlets, run the following commands:

    1. Get-AzVMImagePublisher
    2. Get-AzVMImageOffer
    3. Get-AzVMImageSku
    4. Get-AzVMImage

    The important parameters are listed below

    1. -Location <String>: Specify the location of the VM Image
    2. -PublisherName <String>: You can get the publisher name using Get-AzVMImagePublisher
    3. -Offer <String>: You can get the offering value by running Get-AzVMImageOffer
    4. -Skus <String>: You can get the SKU value using Get-AzVMImageSKU cmdlet
    • Note: For more information about the cmdlets used in this example, use the Get-Help cmdlet in PowerShell console, or refer to the Microsoft online documentation.

    Az and Az CLI commands

    I will walk-through the VM Image data retrieval process using the Az and Az CLI commands. The following table gives you the details of Az and Az CLI commands. You can see that Az CLI commands ran within the BASH shell. In addition, it also ran from a PowerShell integrated console. These cmdlets are building blocks for Cloud automation.

    Az

    Tasks

    Az CLI

    PS /home/prashanth> Get-AzVMImagePublisher -Location $locName | Where-Object {$_.PublisherName -like ‘*WindowsServer*’}|Select-Object PublisherName

    Note:

    You can run the Get-AzVMImage only in PowerShell interactive shell.

    In order to that, type in pwsh in the “$” Bash prompt. And then type in the Get-AzVMImagePublisher cmdlets

    List the publisher Name in the specific location

    Note: Refer Task 1 image for more details

    PS /home/prashanth> az vm image list-publishers –l eastus –query “[?starts_with(name, ‘MicrosoftWindowsServer’)]”

    Bash:

    prashanth@Azure:~$ az vm image list-publishers -l eastus –query “[?starts_with(name, ‘MicrosoftWindowsServer’)]”

    Note: The output is JSON format. I will discuss the formatting part more in the upcoming article.

    PS /home/prashanth> Get-AzVMImagePublisher -Location eastus | Where-Object {$_.PublisherName -eq ‘MicrosoftWindowsServer’}|Select-Object PublisherName,location|Get-AzVMImageOffer

    Note: The Publisher Name and location are mandatory parameters for Get-AzVMImageOffer. The output PublisherName and location are then piped to Get-AzVMImageOffer.

    You can see the details in the image Task 2.

    List the Image offers

    Note: Refer Task 2 image for more details

    >PARAM1=’eastus’

    >PARAM2=’MicrosoftWindowsServer’

    az vm image list-offers -l $PARAM1 -p $PARAM2 -o table

    In the Bash shell, the parameters are declared as PARAM1 and PARAM2, and then it passed as parameters to az vm image list-offers command.

    Note: In order to run the same command in the PowerShell shell, refer to image Task 2(B).

    $locationName=’eastus’

    $publisherName=’MicrosoftWindowsServer’

    $offerName=’windowsserver’

    Get-AzVMImageSku -Location $locationName -PublisherName $publisherName -Offer $offerName | Select-object Skus

    List the VMImage SKUs.

    In this case, we need to type in the location, publisher name, offering to choose the SKU

    Note: Refer Task 3 image for more details

    $locationName=’eastus’

    $publisherName=’MicrosoftWindowsServer’

    $offerName=’windowsserver’

    az vm image list-skus -l $locationName -f $offerName -p $publisherName -o table

    Note: In this sample, you can see that variables are passed to Az CLI command using PowerShell integrated shell.

    You can refer to the Task(3) image.

    Get-AzVMImage -Location “Central US” -PublisherName “MicrosoftWindowsServer” -Offer “windowsserver” -Skus “2016-Datacenter”

    List all the versions of the VM Image

    Note: Refer Task 4 image for more details

    $locationName=’eastus’

    $publisherName=’MicrosoftWindowsServer’

    $offerName=’windowsserver’

    $sku=’2016-Datacenter’

    az vm image list -l $locationName -f $offerName -p $publisherName -s $sku

    List VM Image publisher - Cloud automation

    Task 1 image: List the publisher name using Az and Az CLI

    Get-AzVMImage: Using PowerShell core(“pwsh”) shell to run the Get-AzVMImagePublisher command form the Bash Shell.

    List VM Image publisher using Az CLI commands

    List VM Image offers using AZ cmdlets

    Task 2 image: List the image offers

    List VM Image offers in a BASH Shell

    List VM Image offerings using AZ cmdlets and parameters

    Task 3 image: List the VM Image SKU model

    Get the VM image list to build VM under Cloud automation framework

    Task 4 image: List the VM image

    Summary

    We see that many tech giants are in the peak of developing cmdlets and rest API methods. In addition, many third-party software providers and cloud vendors are deeply relying on PowerShell as their core Cloud automation framework to manage the services. Many IT automation engineers use PowerShell to automate the tasks. Due to its versatile nature, PowerShell best suits to build DevOps pipelines. Now, we can realize the whole many benefits with PowerShell automation.

    The advent of PowerShell core ensures a much greater experience for Cloud automation that fits well within the already well-established methods, practices, and ecosystem for Linux users.

    It is all about choice with compatibility: your choice of using PowerShell or PowerShell core; Python or APIs or ARM templates to effectively manage private and public cloud environments. Cloud Azure automation provides a framework to build run-books using PowerShell. As Microsoft cloud platform is a highly scalable workflow execution environment, Azure Automation allows you to orchestrate frequent deployment and life cycle management tasks using run-books based on Windows PowerShell Workflow functionality. These run-books are stored in and backed up by Azure. By automating run-books, you can greatly minimize the occurrence of errors and makes cloud automation looks much simpler. I will discuss PowerShell cmdlets more in the upcoming article.

    That’s all for now. I hope you like this article. Please leave the comments below. Stay tuned for more updates…

    Table of contents

    IT Cloud Automation using PowerShell
    Deep dive into IT Cloud Automation using PowerShell
    Getting started with Azure Automation
    Getting started with Azure SQL Database using Azure CLI
    Provisioning SQL Server 2019 Azure Container Instance using PowerShell
    Four different methods to copy your Azure SQL database
    Azure SQL Database vs SQL Server on Azure VMs
    How to provision Azure SQL Database using Ansible
    Quick start guide to Geo-restore in Azure SQL Database
    Different ways to login to Azure automation using PowerShell
    How to perform Azure SQL database Import/Export operations using PowerShell
    How to set up Azure Data Sync between Azure SQL databases and on-premises SQL Server
    Prashanth Jayaram
  • PowerShell

    About Prashanth Jayaram

    I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My specialty lies in designing & implementing High availability solutions and cross-platform DB Migration. The technologies currently working on are SQL Server, PowerShell, Oracle and MongoDB. View all posts by Prashanth Jayaram

    168 Views