Skip to main content

Azure PowerShell

  • cmdlets to work with Azure features in PowerShell
  • installation and other commands here↗️

Basic commands​

  • Connect-AzAccount - prompts for Azure credentials then connects to your Azure subscription
  • Get-AzContext - used to determine currently active subscription
  • Get-AzSubscription - returns list of all subscription names in your account
  • Set-AzContext -Subscription <Subscription ID> - change active subscription to given Subscription ID
  • Get-AzResourceGroup - returns list of all Resource Groups in the active subscription; optionally, do Get-AzResourceGroup | Format-Table for a more concise view
  • New-AzResourceGroup -Name <name> -Location <location> - create a new resource group
  • Get-AzResource - returns list of your Azure resources
  • New-AzVM -ResourceGroupName <resource group name> -Name <machine name> -Credential <credentials object> -Location <location> -Image <image name> - create a new virtual machine
  • Remove-AzVM - delete an Azure virtual machine
  • Start-AzVM - starts a stopped VM
  • Stop-AzVM - stops a running VM
  • Restart-AzVM - restarts a VM
  • Update-AzVM - updates the configuration for a VM
  • Get-AzVM -Status - returns list of VMs in active subscription
  • Example VM Creation:
New-AzVM -ResourceGroupName <resource group name> -Name "testvm-auseast-01" -Location "australiasoutheast" -Image "Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest" -Credential (Get-Credential) -OpenPorts 22 -PublicIpAddressName "testvmip-auseast-01"
  • Create variable for easier reference: $vm = (Get-AzVM -Name <vm name> -ResourceGroupName <resource group name>)
  • To cleanup, after deleting VM, delete network interface, managed OS disks, virtual network, network security group and public IP address:
$vm | Remove-AzNetworkInterface –Force
Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.OSDisk.Name | Remove-AzDisk -Force
Get-AzVirtualNetwork -ResourceGroupName $vm.ResourceGroupName | Remove-AzVirtualNetwork -Force
Get-AzNetworkSecurityGroup -ResourceGroupName $vm.ResourceGroupName | Remove-AzNetworkSecurityGroup -Force
Get-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName | Remove-AzPublicIpAddress -Force

PowerShell Scripts​

  • reference↗️
  • file extension .ps1
  • execute scripts in command line using: .\myScript.ps1
  • declare a variable using $ and assign a value using =. For example $loc = "East US" You may also use variables to hold objects returned by cmdlets. For example, $adminCredential = Get-Credential To get the value stored in a variable, use $ and its name.
  • use parameters by passing arguments on the command line. For example, in the command line, call .\setupEnvironment.ps1 -size 5 -location "East US" and inside the script, get the parameter values through variables param([string]$location, [int]$size).
  • Example script:
setupEnvironment.ps1
param([string]$resourceGroup)

$adminCredential = Get-Credential -Message "Enter a username and password for the VM administrator."

For ($i = 1; $i -le 3; $i++)
{
$vmName = "ConferenceDemo" + $i
Write-Host "Creating VM: " $vmName
New-AzVm -ResourceGroupName $resourceGroup -Name $vmName -Credential $adminCredential -Image Canonical:0001-com-ubuntu-server-focal:20_04-lts:latest
}

Then run using ./ConferenceDailyReset.ps1 <resource group name>