Controlling The Flow Of PS Functions Flashcards
Comparison operator syntax
Variables
$variable = “value”
$variable = “value”
If($variable -eq $comparevariable)
Greater than comparison operator
Variables
$variable = “1”
$variable = “2”
If($variable -gt $comparevariable)
Like comparison operator
Variables
$variable = “value”
$variable = “value”
If($variable -like $comparevariable)
Match comparison value
Variables
$variable = “value”
$variable = “value”
If($variable -match $comparevariable)
What are collection operators?
Variables
Each item in collection is evaluated, and the operator returns every value that is true.
$array = 1..10
$comparevariable = 6
If($array -gt $comparevariable)
If($array -contains $comparevariable)
If($comparevariable -in $array)
What are logical operators?
Used to invert or combine other expressions. You can connect multiple statements, allowing you to use a single expression to test for numerous conditions.
What is a ternary operator?
IF one equals two, question mark = True and colon = False
A new syntax for IF / ELSE statements
- Condition
- IF true
- IF false
($variableOne -eq $variableTwo) ? $true : $false
($variableOne -eq $variableTwo) ?
(“$variableOne : $variableTwo”) : (“$variableOne ; $variableTwo”)
How to create an array from 1 to 10?
$array = 1..10
If an array is created 1 - 10, and the variable compare is created with a value of 6, how would you compare that the value in compare is found in the array in two different ways?
If($array -contains $compare) {
Write-Host “Compared” }
If($compare -in $array) [
Write-Host “Compared” }
You have an array of 1 to 10, and a variable compare with a value of 8. Write a script to check if variable compare is included in the array, and if the variable compare is greater than 8. If it is output Yes, if it is not, output No.
If(($compare -in $array) -and ($compare -gt 8)) { Write-Host “Yes” } else { Write-Host “No” }
Considering variable1 = 1 and variable2 = 3, write a script using the new scripting version that determines if variable1 is equal to variable2. If it is output true, if it is not, output false.
If($variable1 -eq $variable2) ? $true : $false
What is a loop?
A construct that allows you to produce a sequence of instructions inside code, iterating several times as long as the defined condition is met
What is a for loop?
Iterates through commands a specified number of time
Easy as you commonly know the number of time to loop ahead of time
What is a while loop?
Repeats a command or a group of commands when the given condition is true
Entry controlled loop
Test condition before the execution of the first iteration
What is a do-while loop?
Condition/Do-While is executed first, then the condition test is at the end of the loop
Exit controlled loop
Tests the condition after the execution of the first iteration.