Exercise - Scripting
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.
Open the Azure Cloud Shell, select Settings, and then Go to Classic version.
Enter
pwshin a terminal window to start a PowerShell session:pwshRun 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.…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 -ForceThe
-Forceswitch overwrites existing content, so be careful if you run this command locally and have an existing profile.Run
pwshto 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.
Ensure you have an existing PowerShell session running. In the console window, enter this code:
$PI = 3.14Create 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.ps1Add 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"Run the script by specifying the path to it:
./PI.ps1Your output displays the following text:
The value of $PI is now 3, inside the scriptYour script does two things. First, it creates a script-local variable
$PIthat shadows the$PIvariable defined in the local scope. Next, the second row in the script interpolates the$PIvariable because you used double quotation marks. It escapes interpolation the first time because you used a back tick.Enter
$PIin the console window:3.14The value is still 3.14. The script didn't change the value.