Manage Azure Resources with PowerShell
Introduction
Azure PowerShell provides a powerful and interactive way to manage your Azure resources. This module allows you to script and automate common Azure tasks, from deploying virtual machines to managing storage accounts and configuring networks.
This document will guide you through the fundamental cmdlets for managing resources in Azure using PowerShell.
Prerequisites
- An Azure subscription. If you don't have one, you can create a free account.
- Azure PowerShell module installed. If you haven't installed it, follow the instructions on the Get Started with Azure PowerShell page.
Connecting to Azure
Before you can manage resources, you need to connect to your Azure account. Use the Connect-AzAccount cmdlet:
Connect-AzAccount
This will prompt you to sign in to your Azure account. Once authenticated, you can select the subscription you want to work with if you have multiple.
To list your available subscriptions and set the current one:
# List all subscriptions
Get-AzSubscription
# Set a specific subscription as the current one
Set-AzContext -SubscriptionId "YOUR_SUBSCRIPTION_ID"
Managing Resource Groups
Resource groups are logical containers for your Azure resources. They help you organize and manage related resources.
Creating a Resource Group
New-AzResourceGroup -Name "MyResourceGroup" -Location "East US"
Listing Resource Groups
Get-AzResourceGroup
Removing a Resource Group
Remove-AzResourceGroup -Name "MyResourceGroup"
Managing Virtual Machines
Virtual machines (VMs) are a core component of Azure infrastructure.
Creating a Virtual Machine
Creating a VM involves several steps, often including defining a network, storage, and the VM configuration itself. Here's a simplified example:
# Define VM parameters
$vmConfig = @{
ResourceGroupName = "MyResourceGroup"
Location = "East US"
VMName = "MyVM"
ImageName = "Win2019Datacenter"
VirtualNetworkName = "MyVNet"
SubnetName = "MySubnet"
PublicIpAddressName = "MyPublicIP"
Credential = Get-Credential
}
# Create the VM
New-AzVM @vmConfig
Listing Virtual Machines
Get-AzVM -ResourceGroupName "MyResourceGroup"
Starting/Stopping a Virtual Machine
# Start a VM
Start-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM"
# Stop a VM
Stop-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM" -Force
Removing a Virtual Machine
Remove-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM" -Force
Managing Storage Accounts
Azure Storage accounts provide a scalable and secure way to store data.
Creating a Storage Account
New-AzStorageAccount -ResourceGroupName "MyResourceGroup" -Name "mystorageaccount" -Location "East US" -SkuName "Standard_LRS"
Listing Storage Accounts
Get-AzStorageAccount -ResourceGroupName "MyResourceGroup"
Getting Storage Account Keys
Get-AzStorageAccountKey -ResourceGroupName "MyResourceGroup" -Name "mystorageaccount"
Removing a Storage Account
Remove-AzStorageAccount -ResourceGroupName "MyResourceGroup" -Name "mystorageaccount"
Further Learning
This is a basic overview of managing Azure resources with PowerShell. For more advanced scenarios and a comprehensive list of cmdlets, refer to the official Azure PowerShell documentation: