18.3. PowerShell and CLI Reference
The landscape of cloud computing has been revolutionized by the integration of command-line tools that allow administrators and developers to script and automate their tasks. In the realm of Microsoft Azure, PowerShell and the Azure Command-Line Interface (CLI) are two of the most potent tools available to manage Azure resources. Understanding these tools and knowing how to leverage their capabilities is essential for efficient cloud management and is also a critical area of knowledge for anyone preparing for the Microsoft Azure Fundamentals (AZ-900) exam.
PowerShell in Azure
Azure PowerShell is a module offering cmdlets to manage Azure through Windows PowerShell and PowerShell Core (cross-platform). It’s designed for users who want to incorporate the command-line tasks of managing Azure resources into their scripting workflows.
Getting Started with Azure PowerShell
To start using Azure PowerShell, you must have PowerShell installed on your system, followed by the installation of the Azure PowerShell module. You can install the Azure PowerShell module using the following command:
Once installed, you can start using it by logging in to your Azure account with Connect-AzAccount cmdlet and then performing various operations on Azure resources.
Key PowerShell Cmdlets for Azure
Here’s a rundown of some key cmdlets and their functions:
● Get-AzResource: Retrieves Azure resources.
● New-AzVm: Creates a new virtual machine.
● Set-AzResourceGroup: Modifies a resource group.
● Remove-AzResourceGroup: Deletes a resource group.
● Get-AzStorageAccount: Retrieves Azure Storage accounts.
Each cmdlet in PowerShell is designed to perform a specific task, and by combining these cmdlets, you can script complex workflows to automate various aspects of Azure management.
Scripting with PowerShell
PowerShell scripting is an advanced way to manage Azure resources. Scripts are written in the PowerShell scripting language and can range from simple one-liners to complex programs. PowerShell scripts have the .ps1 file extension, and can incorporate complex logic, error handling, and even parallel processing.
Here’s a simple PowerShell script that lists all virtual machines in a given subscription:
PowerShell also supports Azure-specific scripting features, such as Azure Resource Manager (ARM) templates. ARM templates are JSON files that define the resources you need to deploy for your solution. PowerShell can deploy these templates to Azure with the New-AzResourceGroupDeployment cmdlet.