Azure PowerShell Documentation
Getting Started with Azure PowerShell
Azure PowerShell is a set of cmdlets that leverage the .NET framework to manage Azure resources. It allows you to automate Azure deployments and management tasks through scripting.
This documentation will guide you through installing, configuring, and using Azure PowerShell effectively.
Installation
To install Azure PowerShell, you can use the PowerShell Gallery. It's recommended to install the Az module, which contains all the cmdlets for Azure resources.
# Check if PowerShell is running as Administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Warning "This script must be run as Administrator."
Start-Process powershell.exe -ArgumentList "-NoProfile", "-ExecutionPolicy Bypass", "-File `"$PSCommandPath`"" -Verb RunAs
Exit
}
# Install the Az module
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Repository PSGallery -Force
# To install only specific modules, e.g., for VMs:
# Install-Module -Name Az.Compute -Scope CurrentUser -Repository PSGallery -Force
Write-Host "Azure PowerShell Az module installed successfully."After installation, you'll need to connect to your Azure account.
Connect-AzAccountThis command will prompt you to log in to your Azure account via a web browser.
Basic Commands
Here are some fundamental Azure PowerShell cmdlets:
- Get-AzSubscription: Lists all Azure subscriptions you have access to.
- Set-AzContext: Switches your current context to a specific subscription.
- Get-AzResourceGroup: Lists all resource groups in your current subscription.
- New-AzResourceGroup: Creates a new resource group.
# List subscriptions
Get-AzSubscription
# Set context to a specific subscription (replace with your subscription ID)
# Set-AzContext -SubscriptionId "YOUR_SUBSCRIPTION_ID"
# List resource groups
Get-AzResourceGroup
# Create a new resource group
New-AzResourceGroup -Name "MyResourceGroup" -Location "East US"Managing Resources
You can manage various Azure resources using specific modules. For example, managing Virtual Machines:
- Get-AzVM: Lists all virtual machines.
- New-AzVM: Creates a new virtual machine.
- Stop-AzVM: Stops a virtual machine.
- Start-AzVM: Starts a virtual machine.
- Remove-AzVM: Deletes a virtual machine.
# Get all VMs in a resource group
Get-AzVM -ResourceGroupName "MyResourceGroup"
# Create a new VM (simplified example)
# New-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM" -Location "East US" -Image win2019datacenter
# Stop a VM
# Stop-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM"Scripting and Automation
Azure PowerShell excels at automation. You can write scripts to perform complex tasks, provision infrastructure, and manage configurations.
Consider using loops, conditional statements, and functions to build robust automation scripts.
# Example: Script to create multiple storage accounts
$locations = @("East US", "West US")
$resourceGroupName = "AutomationRG"
# Ensure the resource group exists
if (-not (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue)) {
New-AzResourceGroup -Name $resourceGroupName -Location "East US"
Write-Host "Resource group '$resourceGroupName' created."
}
foreach ($location in $locations) {
$storageAccountName = "mystorage" + (Get-Random)
Write-Host "Creating storage account '$storageAccountName' in '$location'..."
New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -Location $location -SkuName Standard_LRS
Write-Host "Storage account '$storageAccountName' created successfully."
}Advanced Topics
Explore topics like:
- Working with Azure Functions: Triggering PowerShell scripts from Azure Functions.
- Managed Identities: Securely accessing Azure resources without storing credentials.
- Azure Resource Manager (ARM) integration: Deploying ARM templates with PowerShell.
- Error Handling and Logging: Implementing robust error management in scripts.
Common Cmdlets Reference
| Cmdlet | Description | Module |
|---|---|---|
Get-AzVM |
Retrieves virtual machines. | Az.Compute |
New-AzVM |
Creates a new virtual machine. | Az.Compute |
Get-AzStorageAccount |
Retrieves storage accounts. | Az.Storage |
New-AzStorageAccount |
Creates a new storage account. | Az.Storage |
Get-AzNetworkInterface |
Retrieves network interfaces. | Az.Network |
Get-AzPublicIpAddress |
Retrieves public IP addresses. | Az.Network |
Invoke-AzVMRunCommand |
Runs a script on a virtual machine. | Az.Compute |
For a comprehensive list, visit the official Azure PowerShell documentation.