Exercise - Scripting

Completed

In this unit, you use Azure Cloud Shell on the right side of your screen as your Linux terminal. Azure Cloud Shell is a shell you can access through the Azure portal or at https://shell.azure.com. You don't have to install anything on your computer to use it.

First, you learn how to set up a profile so you can customize your working environment the way you want it. Then, you write some basic scripts and get a feeling for interpolation and scope.

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

In this unit, you use Azure Cloud Shell as a terminal. You can access Cloud Shell through the Azure portal or the Cloud Shell sign-in. You don't have to install anything on your PC or laptop to use it.

Set up a profile

A profile is a script that runs when you start a new session. Having a customized environment can make you more productive.

  1. Open the Azure Cloud Shell, select Settings, and then Go to Classic version.

  2. Enter pwsh in a terminal window to start a PowerShell session:

    pwsh
    
  3. Run this command:

    $Profile | Select-Object *
    

    The output displays something similar to this text:

    CurrentUserAllHosts                        CurrentUserCurrentHost
    -------------------                        ----------------------
    /home/<user>/.config/PowerShell/profile.ps1 /home/<user>/.config/PowerShell/Microsoft.…
    
  4. Create a profile for the current user and the current host by running the command New-Item:

    New-Item `
      -ItemType "file" `
      -Value 'Write-Host "Hello <replace with your name>, welcome back" -foregroundcolor Green ' `
      -Path $Profile.CurrentUserCurrentHost -Force
    

    The -Force switch overwrites existing content, so be careful if you run this command locally and have an existing profile.

  5. Run pwsh to create a new shell. You should now see the following (in green):

    Hello <your name>, welcome back
    

Create and run a script

Now that you have a profile set up, it's time to create and run a script.

  1. Ensure you have an existing PowerShell session running. In the console window, enter this code:

    $PI = 3.14
    
  2. Create a file named PI.ps1 in the current directory and open it in your code editor:

    New-Item -Path . -Name "PI.ps1" -ItemType "file"
    code PI.ps1
    
  3. Add the following content to the file and save it. You can use CTRL+S on Windows and Linux or CMD+S on Mac to save your file.

    $PI = 3
    Write-Host "The value of `$PI is now $PI, inside the script"
    
  4. Run the script by specifying the path to it:

    ./PI.ps1
    

    Your output displays the following text:

    The value of $PI is now 3, inside the script
    

    Your script does two things. First, it creates a script-local variable $PI that shadows the $PI variable defined in the local scope. Next, the second row in the script interpolates the $PI variable because you used double quotation marks. It escapes interpolation the first time because you used a back tick.

  5. Enter $PI in the console window:

    3.14
    

    The value is still 3.14. The script didn't change the value.