Exercise - Create an Azure Resource interactively with Azure PowerShell

Completed

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:

  1. Use the New-AzVM cmdlet 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-Credential cmdlet to set the VM administrator credentials.
    • Add the OpenPorts parameter with port 22 for 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 @azVmParams
    

    Replace 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).

  2. 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.

  3. Wait for the VM creation:

    The VM creation process takes a few minutes to finish.

  4. 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 myResourceGroupName
    
  5. View information about the VM:

    To view information about the VM, display the contents of the variable.

    $vm
    

    Example 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}
    ...
    
  6. Inspect VM properties:

    You can inspect complex objects through the member-access operator (.). For example, to see the properties in the VMSize object associated with the HardwareProfile section, run the following command:

    $vm.HardwareProfile
    

    Or, to get information on one of the disks, run the following command:

    $vm.StorageProfile.OsDisk
    
  7. Get available VM sizes:

    Pass the VM object into other cmdlets to get available sizes:

    $vm | Get-AzVMSize
    
  8. Get 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-01
    
  9. Connect 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:

  1. Shut down the VM:

    Run the following command:

    Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
    

    Enter Y and press Enter when prompted to continue.

  2. Delete the VM:

    Once the VM stops, delete it by running the Remove-AzVM cmdlet.

    Remove-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
    

    Enter Y and press Enter when prompted to continue.

  3. List all resources in the resource group:

    Use the Get-AzResource cmdlet to list all the resources in the resource group. The results are piped to Select-Object to return specific properties:

    Get-AzResource -ResourceGroupName $vm.ResourceGroupName | 
        Select-Object -Property Name, ResourceType, ResourceGroupName
    

    You 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                 myResourceGroupName
    

    The Remove-AzVM command only deletes the VM. It doesn't clean up any of the other resources. To manually clean them up, follow these steps:

  4. Delete the network interface:

    Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name |
        Remove-AzNetworkInterface
    

    Enter Y and press Enter when prompted to continue.

  5. Delete the network security group:

    Get-AzNetworkSecurityGroup -ResourceGroupName $vm.ResourceGroupName |
        Remove-AzNetworkSecurityGroup
    

    Enter Y and press Enter when prompted to continue.

  6. Delete the public IP address:

    Get-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName |
        Remove-AzPublicIpAddress
    

    Enter Y and press Enter when prompted to continue.

  7. Delete the virtual network:

    Get-AzVirtualNetwork -ResourceGroupName $vm.ResourceGroupName |
        Remove-AzVirtualNetwork
    

    Enter Y and press Enter when prompted to continue.

  8. Delete the managed OS disks:

    Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.OSDisk.Name |
        Remove-AzDisk
    

    Enter Y and press Enter when prompted to continue.

  9. 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.