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 subscriptionGet-AzContext- used to determine currently active subscriptionGet-AzSubscription- returns list of all subscription names in your accountSet-AzContext -Subscription <Subscription ID>- change active subscription to given Subscription IDGet-AzResourceGroup- returns list of all Resource Groups in the active subscription; optionally, doGet-AzResourceGroup | Format-Tablefor a more concise viewNew-AzResourceGroup -Name <name> -Location <location>- create a new resource groupGet-AzResource- returns list of your Azure resourcesNew-AzVM -ResourceGroupName <resource group name> -Name <machine name> -Credential <credentials object> -Location <location> -Image <image name>- create a new virtual machineRemove-AzVM- delete an Azure virtual machineStart-AzVM- starts a stopped VMStop-AzVM- stops a running VMRestart-AzVM- restarts a VMUpdate-AzVM- updates the configuration for a VMGet-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-CredentialTo 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 variablesparam([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>