Exercise - Create an Azure Resource interactively with Azure PowerShell
In the original scenario, you must create virtual machines (VMs) to test your Customer Relationship Management (CRM) software. When a new build is available, you want to spin up a new VM to test the entire installation experience from a clean image. Once testing is complete, you can delete the VM.
Let's try the commands to create a VM.
Note
This exercise is optional. If you want to complete this exercise, you'll need to create an Azure subscription before you begin. If you don't have an Azure account or you don't want to create one at this time, you can read through the instructions so you understand the information that's being presented.
Note
You need to use a resource group to complete the steps in this exercise. You can use a resource group that you already created, or you can create a new resource group specifically for this exercise. If you choose to create a new resource group, that will make it easier to clean up any resources that you create as you complete the exercise. If you don't have an existing resource group or you want to create a new one specifically for this exercise, you can follow the steps in Use the Azure portal and Azure Resource Manager to manage resource groups to create a resource group by using the Azure portal, or you can follow the steps in Manage Azure resource groups by using Azure CLI to create a resource group by using the the Azure CLI.
Create a Linux VM with Azure PowerShell
Here's how to create a new Azure VM with Azure PowerShell:
Use the
New-AzVMcmdlet to create the VM.- Specify the resource group for your VM.
- Name the VM, following your organization's naming standards.
- Choose a location close to you from the list of available Azure regions.
- Use the Ubuntu Linux image:
Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest. - Use the
Get-Credentialcmdlet to set the VM administrator credentials. - Add the OpenPorts parameter with port
22for SSH access. - Create a public IP address name for SSH sign-in.
$azVmParams = @{ ResourceGroupName = 'myResourceGroupName' Name = 'testvm-eus-01' Credential = (Get-Credential) Location = 'eastus' Image = 'Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest' OpenPorts = 22 PublicIpAddressName = 'testvm-eus-01' } New-AzVm @azVmParamsReplace myResourceGroupName in the preceding example with the name of an existing resource group, or the name of the resource group that you created for this exercise.
Tip
You can use the Copy button to copy commands to the clipboard. To paste, right-click on a new line in the Cloud Shell terminal and select Paste, or use the Shift+Insert keyboard shortcut (⌘+V on macOS).
Enter Credentials:
When prompted, enter a username and password, following the guidelines: passwords must be 12-123 characters long and meet three of the following four complexity requirements: lowercase characters, uppercase characters, digits, and special characters (Regex match [\W_]). For more information, see Linux VM FAQ.
Wait for the VM creation:
The VM creation process takes a few minutes to finish.
Query the VM:
When complete, query the VM and assign the VM object to a variable (
$vm).$vm = Get-AzVM -Name testvm-eus-01 -ResourceGroupName myResourceGroupNameView information about the VM:
To view information about the VM, display the contents of the variable.
$vmExample output:
ResourceGroupName : myResourceGroupName Id : /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/testvm-eus-01 VmId : 00000000-0000-0000-0000-000000000000 Name : testvm-eus-01 Type : Microsoft.Compute/virtualMachines Location : eastus Tags : {} HardwareProfile : {VmSize} NetworkProfile : {NetworkInterfaces} OSProfile : {ComputerName, AdminUsername, LinuxConfiguration, Secrets} ProvisioningState : Succeeded StorageProfile : {ImageReference, OsDisk, DataDisks} ...Inspect VM properties:
You can inspect complex objects through the member-access operator (
.). For example, to see the properties in theVMSizeobject associated with the HardwareProfile section, run the following command:$vm.HardwareProfileOr, to get information on one of the disks, run the following command:
$vm.StorageProfile.OsDiskGet available VM sizes:
Pass the VM object into other cmdlets to get available sizes:
$vm | Get-AzVMSizeGet the public IP address:
Retrieve the public IP address to connect to the VM and store it in a variable.
$ip = Get-AzPublicIpAddress -ResourceGroupName myResourceGroupName -Name testvm-eus-01Connect to the VM:
Connect to the VM with SSH using the IP address from the variable. For example, if the username is
bob, use the following command:ssh bob@$($ip.IpAddress)Sign out by typing exit.
Delete a VM
To try more commands, let's delete the VM. Follow these steps:
Shut down the VM:
Run the following command:
Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupNameEnter Y and press Enter when prompted to continue.
Delete the VM:
Once the VM stops, delete it by running the
Remove-AzVMcmdlet.Remove-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupNameEnter Y and press Enter when prompted to continue.
List all resources in the resource group:
Use the
Get-AzResourcecmdlet to list all the resources in the resource group. The results are piped toSelect-Objectto return specific properties:Get-AzResource -ResourceGroupName $vm.ResourceGroupName | Select-Object -Property Name, ResourceType, ResourceGroupNameYou should see several resources, including disks, virtual networks, etc., that still exist:
Name ResourceType ResourceGroupName ---- ------------ ----------------- cloudshell Microsoft.Storage/storageAccounts myResourceGroupName testvm-eus-01 Microsoft.Network/virtualNetworks myResourceGroupName testvm-eus-01 Microsoft.Network/publicIPAddresses myResourceGroupName testvm-eus-01 Microsoft.Network/networkSecurityGroups myResourceGroupName testvm-eus-01 Microsoft.Network/networkInterfaces myResourceGroupName testvm-eus-01_OsDisk_1 Microsoft.Compute/disks myResourceGroupNameThe
Remove-AzVMcommand only deletes the VM. It doesn't clean up any of the other resources. To manually clean them up, follow these steps:Delete the network interface:
Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name | Remove-AzNetworkInterfaceEnter Y and press Enter when prompted to continue.
Delete the network security group:
Get-AzNetworkSecurityGroup -ResourceGroupName $vm.ResourceGroupName | Remove-AzNetworkSecurityGroupEnter Y and press Enter when prompted to continue.
Delete the public IP address:
Get-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName | Remove-AzPublicIpAddressEnter Y and press Enter when prompted to continue.
Delete the virtual network:
Get-AzVirtualNetwork -ResourceGroupName $vm.ResourceGroupName | Remove-AzVirtualNetworkEnter Y and press Enter when prompted to continue.
Delete the managed OS disks:
Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.OSDisk.Name | Remove-AzDiskEnter Y and press Enter when prompted to continue.
Verify all resources were removed:
Check the resource group to ensure all resources are removed:
Get-AzResource -ResourceGroupName $vm.ResourceGroupName | Select-Object -Property Name, ResourceType, ResourceGroupName
While you executed these commands interactively, a better approach is to write a PowerShell script. Scripts allow you to reuse the logic for creating or deleting a VM in the future
Next, let's look at how to automate these tasks using a PowerShell script.