Exercise - Create Azure Resources with Azure PowerShell using a script

Completed

In this unit, you continue with the example of a company that develops Linux admin tools. The goal is to use Linux virtual machines (VMs) to allow potential customers to test your software. With a resource group already set up, it's time to create the VMs.

Your company secured a booth at a large Linux trade show. You plan to set up a demo area with three terminals, each connected to a separate Linux VM. You must delete the VMs and re-create them at the end of each day so they start fresh every morning. Creating the VMs manually after a long day is error-prone, so you need to write a PowerShell script to automate the VM creation process.

Write a script to create virtual machines

Follow these steps to write a script in Azure Cloud Shell that automates the creation of virtual machines.

Note

Usually, you'd authenticate to Azure using your credentials with Connect-AzAccount, but in Cloud Shell, you're already authenticated, so this step is unnecessary.

  1. Switch to your home folder:

    Set-Location -Path $HOME
    
  2. Create a new PowerShell script file:

    New-Item -Name ConferenceDailyReset.ps1 -ItemType File
    
  3. Open the integrated Visual Studio Code (VS Code) editor:

    code ./ConferenceDailyReset.ps1
    

    Tip

    The integrated Cloud Shell editor also supports vim, nano, and emacs if you prefer to use one of those editors.

  4. Define a parameter for your resource group name:

    Add the following line to your script:

    param (
        [string]$ResourceGroupName
    )
    
  5. Prompt for VM administrator credentials:

    $adminCredential = Get-Credential -Message 'Enter a username and password for the VM administrator.'
    
  6. Create a loop to execute three times:

    $vms = 'web','app','sql'
    foreach ($vm in $vms) {
        $vm
    }
    
  7. In the loop, return the name for each VM:

    Write-Output "Creating VM: $vm"
    
  8. Create a VM using the $vm variable:

    $azVmParams = @{
        ResourceGroupName = $ResourceGroupName
        Name              = $vm
        Credential        = $adminCredential
        Image             = 'Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest'
        OpenPorts         = 22
    }
    New-AzVm @azVmParams
    
  9. Save the file:

    To save the script, use the ellipsis (...) context menu at the top-right corner of the editor or the Ctrl + S keyboard shortcut.

Completed script

The completed script should look like the following example:

param (
    [string]$ResourceGroupName
)

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

$vms = 'web','app','sql'

foreach ($vm in $vms) {

    Write-Output "Creating VM: $vm"

    $azVmParams = @{
        ResourceGroupName = $ResourceGroupName
        Name              = $vm
        Credential        = $adminCredential
        Image             = 'Canonical:0001-com-ubuntu-server-jammy:22_04-lts:latest'
        OpenPorts         = 22
    }
    New-AzVm @azVmParams
}

Once you confirm your script looks like the code in the previous example, close the editor using the ellipsis (...) context menu at the top-right corner of the editor, or the Ctrl + Q keyboard shortcut.

Run the script

  1. Execute the script using the following command:

    ./ConferenceDailyReset.ps1 -ResourceGroupName <rgn>[sandbox resource group name]</rgn>
    
  2. Wait for completion. The script takes several minutes to complete.

  3. Verify the VMs. Once the script finishes, verify it completed successfully by listing the VMs in the resource group:

    Get-AzVM -ResourceGroupName <rgn>[sandbox resource group name]</rgn>
    

    You should see three VMs, each with a unique name.

You successfully created a script that automates the creation of three VMs, each in a specific resource group, ensuring they're ready for daily demos at the trade show. Although the script is short and straightforward, it significantly speeds up a process that would otherwise be time-consuming and error-prone if performed manually through the Azure portal.