Exercise 3 – creating resources using PowerShell from Cloud Shell
In this section, we’ll learn how to interact with Azure environments using PowerShell as the CLI tool and Cloud Shell as the shell environment for running commands.
The following subsections will show you how to create the necessary resources. These have been segregated into tasks for ease of understanding:
Task – accessing the Azure portal
- Log in to the Azure portal by going to https://portal.azure.com. Alternatively, you can use the Azure desktop app: https://portal.azure.com/App/Download.
Task – setting up Cloud Shell (skip this task if you’ve already completed it)
2. Click on the Cloud Shell code icon on the top toolbar of the portal; if this is the first time you have used Cloud Shell and no storage has been mounted to persist files, you will be prompted to create a storage account. Select your subscription and then click on Create storage:
Figure 6.24 – Cloud Shell setup
3. Once the storage account has been created, you will be connected to a terminal running the shell environment you will use to run commands.
Task – creating a resource group
- PowerShell should be set as the CLI within Cloud Shell for this exercise.
- In this exercise, we will use an example location; you may use a location that meets your needs. You can use the following command to find a location to use to create resources in:
Get-AzLocation
6. To create a resource group, enter the following command:
New-AzResourceGroup -Name Project1-RG -Location “UK South”
The output will look as follows:
Figure 6.25 – Creating a resource group
- To show the resource group that has just been created, enter Get-AzResourceGroup or Get-AzResourceGroup | Format-Table.
- The created resource group will also appear in the portal UI.
Figure 6.26 – Showing resource groups
Task – creating a virtual machine
9. To create a virtual machine, enter the following code:
New-AzVm `
-ResourceGroupName “myResourceGroup” `
-Name “MyVM” `
-Location “UK South” `
-VirtualNetworkName “myVnet”`
-SubnetName “mySubnet” `
-SecurityGroupName “myNetworkSecurityGroup” `
10. When prompted, enter a username and password.
Figure 6.27 – Creating a virtual machine
11. You will see a progress message while the virtual machine is being created.
Figure 6.28 – Virtual machine creation progress
12. Once the virtual machine has been created, you will see a ProvisioningState of Succeeded.
Figure 6.29 – Virtual machine created
13. To show the virtual machine that was just created, enter the following command:
get-AzVM
14. The created virtual machine will also appear in the portal UI.
Figure 6.30 – Showing the created virtual machine
In this exercise, we looked at creating a resource group and a virtual machine using PowerShell via Cloud Shell. In the following exercise, we will create a resource group and a virtual machine using the Azure CLI from Cloud Shell.