Automate Azure tasks with PowerShell -Part1

0
439

Creating administrative scripts is a strong technique to improve the efficiency of your workflow. You can automate typical, repetitive operations, and once a script is confirmed, it will execute regularly, thereby decreasing mistakes.

Say you need to select a tool to manage the Azure resources that will be used to test your Customer Relationship Management (CRM) system. Your tests require the creation of resource groups and the provisioning of virtual machines (VMs).

You want something that is simple for administrators to use while yet being powerful enough to automate the installation and configuration of several virtual machines or script a whole application environment. There are several tools accessible; you must select the best one for your tasks.

Azure gives three administrative tools from which to select.

  • The Azure portal
  • The Azure CLI
  • Azure PowerShell

They all provide roughly the same level of control; any work that you can accomplish with one of the tools, you can probably do with the other two as well. All three are multi-platform, supporting Windows, macOS, and Linux. They differ in syntax, setup requirements, and whether or not automation is supported.

Azure portal

The Azure portal is a website that allows you to establish, configure, and modify Azure subscription resources. The portal is a graphical user interface (GUI) that makes it easy to find the material you need and make any necessary adjustments. It also assists you in completing difficult administrative tasks byusing wizards and tooltips.

There is no method to automate repeated operations using the interface. To set up 15 VMs, for example, you might need to do it one at a time by completing the process for each VM. For complicated activities, this can be time-consuming and error-prone.

Azure CLI

The Azure CLI is a cross-platform command-line software that allows you to login to Azure and run administrative commands on Azure resources. To make a virtual machine, for example, run the following command:

11

Azure PowerShell

Azure PowerShell is a PowerShell module that allows you to connect to your Azure subscription and manage resources. PowerShell is required for Azure PowerShell to work. PowerShell offers services such as the shell window and command processing. The Azure Az PowerShell module incorporates Azure-specific commands.

Choose an administrative tool

In terms of the Azure objects and configurations that can be administered and created, the portal, the Azure CLI, and Azure PowerShell are almost equal. They are all cross-platform as well. This means that you will usually examine a number of additional things while making your decision.

  • Automation: You can automate a series of complicated or repetitive tasks with the support of Azure PowerShell and the Azure CLI, but not via the portal.
  • Learning curve: You can work fast without having to learn new commands or syntax.You do not need to learn syntax or memorize instructions to use the Azure portal. You must be familiar with the precise syntax for each command you use in Azure PowerShell and the Azure CLI.
  • Team expertise: If you have used PowerShell to administer Windows.You  will rapidly become acquainted with Azure PowerShell.

Install PowerShell

There are two components which make up Azure PowerShell.

  • The fundamental PowerShell product This is available in two flavors: Windows PowerShell and PowerShell 7.x, both of which can be installed on Windows, macOS, and Linux.
  • Azure Az PowerShell is a Microsoft Azure PowerShell module. This additional module is required to provide Azure-specific commands to PowerShell.

PowerShell Core is installed via a package manager on both Linux and macOS. The recommended package manager changes depending on the operating system and distribution.

12

Creating Azure Resource using scripts in Azure PowerShell in Windows

PowerShell allows you to type commands and have them executed quickly. This is called interactive mode.

When you type a command into PowerShell, it matches the command to a cmdlet and then executes the required operation.

PowerShell cmdlets

A cmdlet is a PowerShell command (pronounced “command-let”). A cmdlet is a command that only affects one feature. The name cmdlet is meant to mean “little command.” Cmdlet writers are urged by convention to make cmdlets basic and single-purpose.

Cmdlets use verb-noun name conventions, such as Get-Process, Format-Table, and Start-Service. There’s also a verb-choice convention: “get” to get data, “set” to insert or update data, “format” to format data, “out” to route output to a destination, and so on.

PowerShell module

Modules containing cmdlets are distributed. A PowerShell Module is a DLL that contains the code for each cmdlet that is accessible. Cmdlets are loaded into PowerShell by loading the module that contains them. The Get-Module command returns a list of loaded modules:

13

Az PowerShell module

Az is the official name for the Azure PowerShell module, which contains cmdlets for interacting with Azure features. It includes hundreds of cmdlets that allow you to control almost every element of any Azure resource. You can use resource groups, storage, virtual machines, Azure Active Directory, containers, machine learning, and other features.

Install the Az PowerShell module

Run the following instructions to install the most recent Azure Az PowerShell module:

  1. Open the Start menu, and enter PowerShell.
  2. Select the PowerShell icon.
  3. Enter the following command, and then press Enter.

29

This will install the module for the current user (controlled by the Scope parameter). Because the script relies on NuGet to get components, you may be required to download and install the most recent version of NuGet, depending on the version you have installed.

14

Enter Y, and then press Enter.

The PowerShell Gallery is not set up as a trusted repository for PowerShellGet by default. When you install a module from an untrusted repository, you will be requested to confirm that you wish to install the module, with the following output:

15

Enter Y or A, and then press Enter.

Import-Module may fail with the following result depending on your security configuration:

16In this case, press Ctrl+C to stop the program.

See you with the next part…

Comments

comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here