Module 3 (1)
Module 3 (1)
Module 3 (1)
As with any scripting language, PowerShell provides a number of languages constructs that let you
control the flow of your script as well as make decisions about what it should do. These language
constructs are the if, switch, for, break, continue, foreach, while, and do statements.
Importing Contents
a. TEXT Files
Once you have a file, you can use PowerShell to import the contents. For this example, will import
the contents of Cities.txt.
The contents have been successfully imported. We can see what type of object we're working with by
piping $Cities to Get-Member.
Looks like we are working with an array of strings. We can see this by using the Count property, as
well as taking a look at the first value in the array, which would be reflected as $Cities[0].
b. CSV Files
Arrays
a. Comma-separated array
When you use , to separate values, PowerShell automatically interprets them as an array. This is a shorthand
way to create an array, and it’s convenient for simple array assignments.
Syntax: $myarr1 = 1, 2, 3
PowerShell, arrays can contain elements of different types. PowerShell arrays are heterogeneous, meaning they
can hold a mix of data types, such as integers, strings, booleans, objects, etc.
For example:
h. JAGGERED ARRAY – a type of multi-dimensional array where size of all rows may be
different.
Jagged and multidimensional arrays are useful for holding lists of lists and arrays of arrays.
• Jagged arrays are arrays of arrays, where each array has only as many elements as it needs.
• A nonjagged array is more like a grid or matrix, where every array needs to be the same size.
• Jagged arrays are much easier to work with (and use less memory), but nonjagged multidimensional
arrays are sometimes useful for dealing with large grids of data.
• Since a jagged array is an array of arrays, creating an item in a jagged array follows the same rules as
creating an item in a regular array.
• If any of the arrays are single-element arrays, use the unary comma operator
i. To create an array of a specific size, use the New-Object cmdlet: explain with example
j. Combining two arrays
Conditions
Decision making structures have one or more conditions to be evaluated or tested by the programme,
along with a statement or statements that are to be executed if the condition is determined to be true,
and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision-making structure found in most of the
programming languages
A. if statement –
An if statement consists of a Boolean expression followed by one or more statements.
B. if-else statement-
An if statement can be followed by an optional else statement, which executes when the
Boolean expression is false
C. nested if statement-
You can use one if or else if statement inside another if or else if statement(s).
D. if-else ladder
//Find the smallest of 3 numbers
if ($x -lt $y) {
if ($x -lt $z) {
Write-Host $x "is the smallest"
} else {
Write-Host $z "is the smallest"
}
} elseif ($y -lt $z) {
Write-Host $y "is the smallest"
} else {
Write-Host $z "is the smallest"
}
E. switch statement-
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each case.
Syntax The syntax of enhanced for loop is;
•Powershell allows you to create secure passwords automatically with a customised script.
• In this topic, you will learn to write a script that helps you to create passwords which comply with
your security policies.
• Moreover, it will allow for simple adjustments if the policies are changing.
1. Get-Random cmdlet specifies the set of items from which random values will be chosen.
2. [byte][char]’character’
3.
• That's it. Read-Host is a simple cmdlet but one that comes in useful when needing to get information
from the script user.
This Prompt parameter allows you to give the script user some kind of indication as to what to input.
For example, if your script requires a server name, you might choose to use Read-Host to prompt the
user to input that when the script is run.
Asking for Passwords
Let us say you have a deep, dark secret you do not want anyone to know about, but you need to pass
this password to some kind of software. When you do not use AsSecureString, you are onto me!
However, if you use AsSecureString, my secret is safe since every character you type is replaced with
an asterisk and the output is saved as a secure string rather than a plain-text string.
HASH TABLES
Hash Tables are unordered data types unlike arrays which are ordered and indexed.
To get the type of hash table
An example we can use a hash table for could be if we have a bunch of employees, with
employee ids as the keys and their names as the respective values.
Another example for hash table could be of an inventory system where the keys could be the
product number or barcode numbers or names and values could be the price of the item.
• Terminating Error: A serious error during execution that halts the command (or script execution)
completely. Examples can include non-existent cmdlets, syntax errors that would prevent a cmdlet
from running, or other fatal errors.
• Non-Terminating Error: A non-serious error that allows execution to continue despite the failure.
Examples include operational errors such file not found, permissions problems, etc.
Try/Catch Blocks:
• The Try and Catch statements allow us to control script flow when we encounter errors.
• The statements behave similarly to the statements of the same name found in C# and other
languages.
• This means Non-terminating (operational) errors inside a try block will not trigger a Catch*.
• If you would like to catch all possible errors (terminating and non-terminating) – then simply set
the error action preference to Stop.
• Remember that Stop error action forces a non-terminating error to behave like a terminating error.
• This variable is a collection of PowerShell Error Objects with the most recent error at index 0.
• On a freshly initialised PowerShell instance (no errors have occurred yet) the $error variable is
ready and waiting as an empty collection: