Skip to main content

Logic Flow for PowerShell Core in Linux

Hands-On Lab

 

Photo of

Training Architect

Length

01:00:00

Difficulty

Intermediate

PowerShell is no different than other computer languages in that more complex capabilities require the addition of logic. For those familiar with logic controls in other programming languages, they'll find no surprises.

In this hands-on lab, we cover some of the PowerShell specifics for controlling logic flow, conditional statements, comparison operators, and switches.

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.

Logic Flow for PowerShell Core in Linux

Introduction

PowerShell is no different than other computer languages in that more complex capabilities require the addition of logic. For those familiar with logic controls in other programming languages, they'll find no surprises.

In this hands-on lab, we cover some of the PowerShell specifics for controlling logic flow, conditional statements, comparison operators, and switches.

Connecting to the Lab

  1. Begin by logging in to the lab server using the credentials provided on the hands-on lab page.

    ssh cloud_user@PUBLIC_IP_ADDRESS

Perform a System Update, Register the MS RedHat Repository, and Install PowerShell

  1. Sync the package index files.

    sudo yum check-update
  2. Perform the system update.

    sudo yum update
  3. Register the Microsoft RedHat repository.

    curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
  4. Install PowerShell.

    sudo yum install -y powershell

Work with PowerShell Conditional Statements

  1. Open PowerShell.

    pwsh
  2. Create a variable equal to 3.

    $a=3
  3. Create a script file.

    vi script1.ps1
  4. Add the following to the file.

    if ($a -gt 2) {
      Write-Host "The value $a is greater than 2."
    }
  5. Save the changes and exit the editor.

  6. Run the script file and verify the results.

    ./script1.ps1
  7. Change the variable value.

    $a=1
  8. Rerun the script file and verify the lack of output.

    ./script1.ps1
  9. Create a new script.

    vi script2.ps1
  1. Add the following to the file.

    if ($a -gt 2) {
      Write-Host "The value $a is greater than 2."
    }
    else {
      Write-Host ("The value $a is less than or equal to 2,")
    }
  2. Save the changes and exit the editor.

  3. Run the new script and verify the output.

    ./script2.ps1
  4. Change the value of the variable.

    $a=3
  5. Rerun the script file and verify the output.

    ./script2.ps1

Work with PowerShell Switches to Handle Multiple If Statements

  1. Enter the following code and verify the output.

    switch (3)
    {
      1 {"It is one."}
      2 {"It is two."}
      3 {"It is three."}
      4 {"It is four."}
    }
  2. Enter the following code and verify the output.

    switch (3)
    {
      1 {"It is one."}
      2 {"It is two."}
      3 {"It is three."}
      4 {"It is four."}
      3 {"Three again."}
    }
  3. Enter the following code and verify the output.

    switch (3)
    {
      1 {"It is one."}
      2 {"It is two."}
      3 {"It is three."; Break}
      4 {"It is four."}
      3 {"Three again."}
    }
  4. Enter the following code and verify the output.

    switch (4, 2)
    {
      1 {"It is one."}
      2 {"It is two."}
      3 {"It is three."}
      4 {"It is four."}
      3 {"Three again."}
    }
  5. Enter the following code and verify the output.

    switch (4, 2)
    {
      1 {"It is one."; Break}
      2 {"It is two."; Break}
      3 {"It is three."; Break}
      4 {"It is four."; Break}
      3 {"Three again."}
    }
  6. Exit PowerShell.

    exit

Conclusion

Congratulations, you've completed this hands-on lab!