Skip to main content

Running Azure PowerShell Cmdlets in PowerShell Jobs

Hands-On Lab

 

Photo of Chad Crowell

Chad Crowell

DevOps Training Architect II in Content

Length

01:45:00

Difficulty

Intermediate

As a systems administrator, having to sit and wait at the terminal for a job to finish is frustrating. In this hands-on lab, we run our PowerShell cmdlets as a background task, so you can get your prompt back and continue with your work. You can then retrieve the job at a later time, allowing you to check the status and perform actions with the resource upon completion.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Running Azure PowerShell Cmdlets in PowerShell Jobs

Introduction

As a systems administrator, having to sit and wait at the terminal for a job to finish is frustrating. In this hands-on lab, we run our PowerShell cmdlets as a background task, so you can get your prompt back and continue with your work. You can then retrieve the job at a later time, allowing you to check the status and perform actions with the resource upon completion.

Log in to the Linux VM

  1. Open a new terminal.

  2. Copy the public IP address provided with this hands-on lab.

  3. Connect to the VM via SSH using the public IP provided on the lab page:

    ssh cloud_user@<PUBLIC_IP_OF_THE_VM>
  4. Once logged in, start the PowerShell prompt:

    pwsh

Install the Az Module and Connect to Azure

  1. Install the module:

    Install-Module -Name Az -AllowClobber -Scope CurrentUser
  2. Enter Y to continue installing from the PowerShell gallery. It will take a couple minutes to finish.

  3. From the PowerShell prompt, connect to Azure:

    Connect-AzAccount
  4. In the browser, navigate to https://microsoft.com/devicelogin.

  5. Enter the code provided in the terminal.

  6. Enter the Azure Portal username and password provided on the lab page.

Create the VM as a Background Task

  1. In the terminal, create a new Azure VM as a job:

    $job = New-AzVM -ResourceGroupName '<RESOURCE_GROUP_IN_THE_LAB>' -Name 'mynewVM' -Location 'westus' -AsJob -ImageName UbuntuLTS

    Retrieve the <RESOURCE_GROUP_IN_THE_LAB> name by logging in to the Azure Portal and navigating to Resource groups via the search.

  2. Make up a username and password for the VM — make sure you remember the password. It will take a few minutes to finish creating.

    Note: The password must be between 12–72 characters long and contain three of the following requirements:

    • An uppercase character
    • A lowercase character
    • A numeric digit
    • A special character
  3. While it's creating in the background, let's create a script:

    vim script.ps1
  4. Paste the following script (first enter :set paste):

    Set-Location /tmp
    
    Get-ChildItem | Where-Object Length -gt 1kb

    Save and quit by hitting Escape followed by :wq!.

  5. Check the status of the VM job:

    Get-Job
  6. Pass the output of the job to a new variable:

    $vm = Receive-Job $job
  7. See all the properties associated with the new VM:

    $vm
  8. Create an $fqdn variable:

    $fqdn = $vm.FullyQualifiedDomainName
  9. Connect to the new VM via SSH using the username and password you created a minute ago:

    ssh <YOUR_USERNAME>@$fqdn

Conclusion

Congratulations on successfully completing this hands-on lab!