Azure PowerShell Tutorial
Introduction
Azure PowerShell is a set of cmdlets for managing Azure resources directly from the PowerShell command line. This tutorial will guide you through the basics of using Azure PowerShell, from installation to executing common commands.
Installation
To start using Azure PowerShell, you need to install the Az module. Open a PowerShell window with administrative privileges and run the following command:
Install-Module -Name Az -AllowClobber -Scope CurrentUser
This command installs the Az module, which is the module used to manage Azure resources.
Connecting to Your Azure Account
Before you can manage Azure resources, you need to authenticate your Azure account. Use the following command to sign in:
Connect-AzAccount
This command opens a web page where you can enter your Azure credentials. Upon successful authentication, you will be connected to your Azure account.
Managing Azure Resources
Listing Azure Resources
To list all the resources in your Azure subscription, use the following command:
Get-AzResource
ResourceGroupName : MyResourceGroup Name : MyResource ResourceType : Microsoft.Web/sites Location : West US
Creating a Resource Group
Resource groups in Azure act as containers that hold related resources. To create a new resource group, use the following command:
New-AzResourceGroup -Name MyResourceGroup -Location "East US"
This command creates a new resource group named MyResourceGroup in the "East US" region.
Creating a Virtual Machine
To create a new virtual machine, use the following command:
New-AzVM -ResourceGroupName MyResourceGroup -Name MyVM -Location "East US" -Image UbuntuLTS -Credential (Get-Credential)
This command creates a new virtual machine named MyVM in the MyResourceGroup resource group, using the UbuntuLTS image. You will be prompted to enter credentials for the VM.
Advanced Usage
Automating Tasks with Scripts
You can automate repetitive tasks by writing PowerShell scripts. Here is an example script that creates a virtual network:
$resourceGroupName = "MyResourceGroup" $location = "East US" $virtualNetworkName = "MyVNet" $subnetName = "MySubnet" # Create a resource group New-AzResourceGroup -Name $resourceGroupName -Location $location # Create a virtual network New-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Location $location -Name $virtualNetworkName -AddressPrefix "10.0.0.0/16" # Create a subnet Add-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix "10.0.1.0/24" -VirtualNetwork (Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName) # Apply the subnet configuration Set-AzVirtualNetwork -VirtualNetwork (Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName)
Managing Storage Accounts
You can manage Azure Storage accounts using Azure PowerShell. Here is an example command to create a new storage account:
New-AzStorageAccount -ResourceGroupName MyResourceGroup -Name mystorageaccount -Location "East US" -SkuName Standard_LRS
This command creates a new storage account named mystorageaccount in the MyResourceGroup resource group with the Standard_LRS SKU.
Conclusion
Azure PowerShell is a powerful tool for managing Azure resources from the command line. This tutorial covered the basics, including installation, authentication, resource management, and automation. With these skills, you can efficiently manage your Azure infrastructure.