Skip to content

Commit

Permalink
[Concept]: Basics (#366)
Browse files Browse the repository at this point in the history
* [Concept]: Basics

- Basics for PowerShell : short history, introductions
- Concept exercise : classic lasagna exercise
- Extra: eternal links for related concepted mentioned

* Fixed wording and folder names

Changes made multiple files regarding the wording based on feedback

* Update config.json

Add "basics" slug to concepts array
  • Loading branch information
glaxxie committed Apr 17, 2024
1 parent cc8c087 commit 631560b
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 2 deletions.
5 changes: 5 additions & 0 deletions concepts/basics/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "PowerShell is a command-line shell and scripting language designed especially for Windows system administration.",
"authors": ["glaxxie"],
"contributors": ["BethanyG", "IsaacG"]
}
92 changes: 92 additions & 0 deletions concepts/basics/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# About

PowerShell is a dynamic and implicitly typed (_but also supporting [strongly typed][strong typed] variables_) scripting language from Microsoft, mostly focused on task automation and configuration management.

PowerShell supports imperative, functional, object-oriented, and even pipeline style programming.

PowerShell was built on the .NET platform and first released in 2006.
It has been included by default in all modern Windows OS, and can also be installed on Mac and Linux.


## Variables

There are two common ways to define a variable in PowerShell.

Variable names in PowerShell must start with a `$`, and use the assignment operator `=` to bind the variable name to a value.

```powershell
> $intVar = 1
> $doubleVar = 2.0
> $stringVar = "Hello"
# Checking a variable type using the GetType() method
> Write-Host $doubleVar.GetType()
System.Double
```

You can also specify a variable's type explicitly, if you want to enforce/require the value be of a certain type.
PowerShell will try its best to convert the value from one type to another if possible.

```powershell
# String represents a floating number can be converted to integer
> [int]$staticOne = "1.234"
> $staticOne
1
# String of letters can not be converted to double type
> [double]$staticTwo = "hello"
MetadataError: Cannot convert value "hello" to type "System.Double"
```

### Constants

Constants in PowerShell are variables that can't be **changed** nor **removed** after definition.
This leads us to the second way of defining variables: using the cmdlet `New-Variable`.

```powershell
> New-Variable -Name "MyConstant" -Value 100 -Option Constant
> $MyConstant
100
```


## Functions

Functions in PowerShell are defined using the `function` keyword.
Each function can have zero or more formal parameters in `()` parenthesis, followed by the function scope starting with `{` and ending with `}`.

```powershell
function Greeting {
return "Hello!"
}
```

Functions in PowerShell can return values explicitly via the `return` keyword, after being called using the syntax `FuncName $Arg`.

```powershell
function AddTwoNumbers($NumOne, $NumTwo):
$result = $NumOne + $NumTwo
return $result
> AddTwoNumbers 10 20
30
```

## Comments

Single comments in PowerShell start with a `#`, and end at line termination.
Comment blocks in PowerShell start with `<#` and end with `#>`.

```powershell
function Main() {
#This is a comment
<#
And this is a comment block
that can span multiple lines
#>
}
```

[strong typed]: https://pscustomobject.github.io/powershell/coding%20habits/powershell%20best%20practice/PowerShell-Strongly-Typed-Variables/
[assignment operator]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators
[New-Variable]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-variable
102 changes: 102 additions & 0 deletions concepts/basics/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Introduction

PowerShell is a dynamic and implicitly typed (_but also supporting [strongly typed][strong typed] variables_) scripting language from Microsoft, mostly focused on task automation and configuration management.

PowerShell supports imperative, functional, object-oriented, and even pipeline style programming.

PowerShell was built on the .NET platform and first released in 2006.
It has been included by default in all modern Windows OS, and can also be installed on Mac and Linux.


## Variables

There are two common ways to define a variable in PowerShell.

Variable names in PowerShell must start with a `$`, and use the assignment operator `=` to bind the variable name to a value.

```powershell
> $intVar = 1
> $doubleVar = 2.0
> $stringVar = "Hello"
# Checking a variable type using the GetType() method
> Write-Host $doubleVar.GetType()
System.Double
```

You can also specify a variable's type explicitly, if you want to enforce/require the value be of a certain type.
PowerShell will try its best to convert the value from one type to another if possible.

```powershell
# String represents a floating number can be converted to integer
> [int]$staticOne = "1.234"
> $staticOne
1
# String of letters can not be converted to double type
> [double]$staticTwo = "hello"
MetadataError: Cannot convert value "hello" to type "System.Double"
```


### Constants

Constants in PowerShell are variables that can't be **changed** nor **removed** after definition.
This leads us to the second way of defining variables: using the cmdlet `New-Variable`.

```powershell
> New-Variable -Name "MyConstant" -Value 100 -Option Constant
> $MyConstant
```

For more detailed and advanced ways to create a new variable, please read [New-Variable][New-Variable].


## Functions

[Functions][functions] in PowerShell are defined using the `function` keyword.
Each function can have zero or more formal parameters in `()` parenthesis, followed by the function scope starting with `{` and ending with `}`.

```powershell
function Greeting {
return "Hello!"
}
```

Functions in PowerShell can return values explicitly via the `return` keyword, after being called using the syntax `FuncName $Arg`.

```powershell
function AddTwoNumbers($NumOne, $NumTwo):
$result = $NumOne + $NumTwo
return $result
> AddTwoNumbers 10 20
30
```

An advanced look of how to write a detailed function definition, and how functions can return values implicitly will be covered in the `Functions` concept module.


## Comments

Single comments in PowerShell start with a `#`, and end at line termination.
Comment blocks in PowerShell start with `<#` and end with `#>`.
An advanced showcase of comment block to create Comment-Based Help will be covered in the `Functions` concept module.

```powershell
function Main() {
#This is a comment
<#
And this is a comment block
that can span multiple lines
#>
}
```


[parameters]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters
[strong typed]: https://pscustomobject.github.io/powershell/coding%20habits/powershell%20best%20practice/PowerShell-Strongly-Typed-Variables/
[assignment operator]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators
[New-Variable]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-variable
[scope]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes
[functions]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions
14 changes: 14 additions & 0 deletions concepts/basics/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"url": "https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return",
"description": "return"
},
{
"url": "https://learn.microsoft.com/en-us/powershell/scripting/community/contributing/powershell-style-guide",
"description": "style guide"
},
{
"url": "https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/cmdlet-overview",
"description": "style guide"
}
]
21 changes: 19 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@
]
},
"exercises": {
"concept": [],
"concept": [
{
"slug": "jeffreys-juicy-lasagna",
"name": "Jeffrey's Juicy Lasagna",
"uuid": "00e439fb-a5f4-4e0c-b89c-ea23c586c9e3",
"concepts": [
"basics"
],
"prerequisites": [],
"status": "wip"
}
],
"practice": [
{
"slug": "hello-world",
Expand Down Expand Up @@ -1147,7 +1158,13 @@
}
]
},
"concepts": [],
"concepts": [
{
"uuid": "5151f982-fa99-494e-9036-af7e6a3af86f",
"slug": "basics",
"name": "Basics"
}
],
"key_features": [
{
"title": "Cross Platform",
Expand Down
42 changes: 42 additions & 0 deletions exercises/concept/jeffreys-juicy-lasagna/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Hints

## General
- A variable's [type][types] is based on the value of that variable: *10* as `Int` , *'Hello'* as `String`, or *2.0* as `double`.

## 1. Define constant expected bake time in minutes

- You need to define a constant variable and assign it an integer value.
- Constants can be defined through the [New-Variable cmdlet][new variable].

## 2. Calculate remaining bake time in minutes

- You need to define a [function][defining functions] with a single parameter.
- Use the [mathematical operator for subtraction][arithmetic] to subtract values.
- This function should [return a value][return].

## 3. Calculate preparation time in minutes

- You need to define a [function][defining functions] with a single parameter.
- Use the [mathematical operator for multiplication][arithmetic] to multiply values.
- You could define an extra _constant_ for the time in minutes per layer rather than using a "magic number" in your code.
- This function should [return a value][return] an integer.

## 4. Calculate total elapsed cooking time (prep + bake) in minutes

- Define a [function][defining functions] with two parameters.
- You can _call_ a function you've defined previously.
- You can use the [mathematical operator for addition][arithmetic] to sum values.
- This function should [return a value][return] an integer.

## 5. Update the recipe with notes

- Define a function without parameter.
- This function should return a string value.

[types]: https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-04
[assignment]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators
[comments]: https://realpython.com/python-comments-guide/
[defining functions]: https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions
[return]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return
[arithmetic]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_arithmetic_operators
[new variable]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-variable
53 changes: 53 additions & 0 deletions exercises/concept/jeffreys-juicy-lasagna/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Instructions

You're going to write some code to help you cook a lasagna from your favorite cookbook.

There are five tasks, all related to the process of cooking your recipe.

## 1. Define expected bake time in minutes

Define an `ExpectedBakeTime` constant that returns how many minutes the lasagna should bake in the oven.
According to your cookbook, the Lasagna should be in the oven for 40 minutes:

```powershell
> $ExpectedBakeTime
> 40
```

## 2. Calculate remaining bake time in minutes

Implement the `RemainingTime` function that takes the actual minutes the lasagna has been in the oven as an argument and returns how many minutes the lasagna still needs to bake based on the `ExpectedBakeTime`.

```powershell
> RemainingTime -Minutes 30
> 10
```

## 3. Calculate preparation time in minutes

Implement the `PreparationTime` function that takes the number of layers you want to add to the lasagna as an argument and returns how many minutes you would spend making them.
Assume each layer takes 2 minutes to prepare.

```powershell
> PreparationTime -Layers 2
> 4
```

## 4. Calculate total elapsed cooking time (prep + bake) in minutes

Implement the `ElapsedTime` function that has two parameters: `Layers` (_the number of layers added to the lasagna_) and `Minutes` (_the number of minutes the lasagna has been baking in the oven_).
This function should return the total number of minutes you've been cooking, or the sum of your preparation time and the time the lasagna has already spent baking in the oven.

```powershell
> ElapsedTime -Layers 3 -Minutes 20
> 26
```

## 5. Create a notification that the lasagna is ready

Define the `Notification` function that does not take any arguments and returns a message indicating that the lasagna is ready to eat.

```powershell
> Notification
> "Lasagna's ready!"
```
Loading

0 comments on commit 631560b

Please sign in to comment.