Az.Compute Module
Manage Azure Compute resources using PowerShell.
Overview
The Az.Compute module provides cmdlets for managing Azure Virtual Machines, Virtual Machine Scale Sets, Availability Sets, and other related compute resources. This module is essential for automating the deployment and management of your compute infrastructure on Azure.
With Az.Compute, you can perform tasks such as:
- Creating, starting, stopping, and deleting virtual machines.
- Configuring networking for virtual machines.
- Managing disks and snapshots.
- Deploying and managing Virtual Machine Scale Sets.
- Working with Availability Sets for high availability.
- Capturing images from virtual machines.
Key Cmdlets
Virtual Machines
Get-AzVM: Gets virtual machines.New-AzVM: Creates new virtual machines.Start-AzVM: Starts virtual machines.Stop-AzVM: Stops virtual machines.Restart-AzVM: Restarts virtual machines.Remove-AzVM: Removes virtual machines.Update-AzVM: Updates virtual machines.Set-AzVMSourceImage: Sets the source image for a virtual machine.
Virtual Machine Scale Sets
Get-AzVmss: Gets virtual machine scale sets.New-AzVmss: Creates new virtual machine scale sets.Update-AzVmss: Updates virtual machine scale sets.Set-AzVmssInstanceCount: Sets the instance count for a virtual machine scale set.Remove-AzVmss: Removes virtual machine scale sets.
Disks and Snapshots
Get-AzDisk: Gets managed disks.New-AzDisk: Creates managed disks.Get-AzSnapshot: Gets snapshots.New-AzSnapshot: Creates snapshots.
Using Az.Compute
To use the cmdlets in the Az.Compute module, you first need to install the Azure PowerShell Az module and connect to your Azure account.
# Install Azure PowerShell Az module (if not already installed)
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
# Connect to your Azure account
Connect-AzAccount
Example: Creating a Virtual Machine
Create a new Linux VM
# Define resource group name and VM name
$resourceGroupName = "MyResourceGroup"
$vmName = "MyVM"
$location = "East US"
# Create a resource group if it doesn't exist
if (-not (Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue)) {
New-AzResourceGroup -Name $resourceGroupName -Location $location
}
# Define VM configuration
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_DS1_v2"
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential (Get-Credential)
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName "Canonical" -Offer "UbuntuServer" -Sku "18.04-LTS" -Version "latest"
# Create the virtual machine
New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig
Start a virtual machine
Start-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM"
Get information about a virtual machine scale set
Get-AzVmss -ResourceGroupName "MyResourceGroup" -VMScaleSetName "MyVmss"
Parameters
Most cmdlets accept common parameters such as:
-ResourceGroupName: The name of the resource group.-Name: The name of the resource (e.g., VM name, disk name).-Location: The Azure region.-Force: Suppresses confirmation prompts.-WhatIf: Shows what would happen if the cmdlet runs.-Confirm: Prompts for confirmation before running the cmdlet.
Common Parameters for VM Creation
| Parameter | Description | Required |
|---|---|---|
| -VMName | Specifies the name of the virtual machine. | Yes |
| -VMSize | Specifies the size of the virtual machine. | Yes |
| -Credential | Specifies a user name and password for the virtual machine. | Yes (for interactive logins) |
| -ImageName | Specifies the name of the operating system image. | Yes |
| -OpenPorts | Specifies the ports to open on the virtual machine. | No |