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.
What is a do-until loop?
Loop repeats until the condition returns true
They are the opposite of the Do-While loop
Do { } Until ( )
what is a foreach-object?
What is a foreach statement or loop?
What is a foreach() method?
What happens with an entry loop?
Test condition is checked BEFORE entering the loop
What happens with an exit loop?
Test condition is checked AFTER executing the loop
Entry vs Exit loop
Variable
$number = 1
While ($number -le 10) { $number; $number++ }
Do { $number; number++ } While($number -le 10)
What is the Do-While syntax?
Do squiggly brackets While parenthesis/condition
Do { command sequence } While (condition)
What are switch statements?
Alternate syntax for doing multiple comparisons with a value
If you have an IF statement with an ELSE….
The ELSE statement is the default is nothing matches
What is basic switch statement syntax?
Switch (expression) {
Condition1 { code }
Condition2 { code }
Condition3 { code }
$number = 3
# the Switch option means in this statement variable number is the number or object you wish to test
# 5, 10, and 20 are the comparison operators or chosen values to test against
Switch ($number) {
5 { Write-Host “Number equals 5” }
10 { Write-Host “Number equals 10” }
20 { Write-Host “Number equals 20” }
Default. { Write-Host “Number is not equal to 5, 10, or 20” }
}
What does Default mean in a Switch statement?
What are foreach enumerators?
Scripting language construct that iterates through COLLECTIONS of items
what is in a foreach loop statement?
Item - the variable contain the current item
Collection - objects such as files, numbers, or other object types
Script Block - sequence of commands to be executed against each item in the collection
What is the base syntax for foreach?
ForEach (Item In Collection) {
CODE or SEQUENCE OF COMMANDS
}
Foreach is used when sorting through a ……
Collection of data
Foreach structure expanded
$collection = 1..3
ForEach ($number in $collection) {
Write-Host “Current Number: $collection”
}
What is $number in:
ForEach ($number in $collection) {
Write-Host “Current Number: $collection”
}
A variable
What is in called in the following:
ForEach ($number in $collection) {
Write-Host “Current Number: $collection”
}
An operator
What is the syntax for a basic PS command?
Verb-Noun -parameter value -anotherparameter another value -switch
What is the parameter, noun, verb, and value of?
Get-EventLog Application -Newest 5
Verb = Get-EventLog
Noun = Application
Parameter = -Newest
Value = 5
What characters are required to group arrays, collections, and lists?
Brackets. [ ]
When you know the command you want to use but need guidance about exactly how to use it, what do you type?
E.g. for Get-Process
Get-Help Get-Process | more