Powershell is a universal automation, scripting and development tool and is developped to automate tasks without programming knowledge
Some interesting commands:
- Get-Alias: shows list of aliases (zal een onderliggende commando representeren), de volledige list krijg je enkel op Windows
- Get-Command: shows list of commands
- Different flavours:
- most are cmdlets, often written in C#
- Functions are commands written in Powershell
- Different flavours:
- Get-Help: Same as man in Linux
- Update-Help: update its help information
These commands also have parameters. These values will change the behaviour of the command. Powershell does not have sudo, so it can be annoying sometimes to use.
$var
The strict mode will prevent you from using the var without defining it first (Set-StrictMode -Version Latest)
has premade variables. You can change these, but maybe dont :)
$null -> represents nothing, but you initialise it. This is used a lot to give a var a value as response of output of a certain function
$LASTEXITCODE -> returns code of last external executable program, 0 = success, anything else does is not a success.
basically same as things we know: strings, int32 (standard), int64, bools, float (32 bits), double (64 bits)...
'' -> no variable expansion "" -> variable expansion _ -> powershell will treat this as a commando
In powershell, everything is an object
- $color, is a string object
Every object has properties -> object -Property
The Get-Member method will show you all the functions for an object
$color_picker = @('blue', 'yellow')
Using + will make a new array Using += will append item to array
ArrayLists are dynamic (vectors) and are better for when you have arrays with heavy objects. Cast your array to an arraylist :)
In an arrayList, + and += do not exist, use Add(), Remove()
$users = @{ Niels = ... }
manipulation -> Add(), ContainsKey(), Remove()
Utilize new-object cmdlet
This object can then be used like anyother object
The output of one command will be piped to another command.
for example: Get-Service | Start-Service
Shows an example
Passing parameters to a command, PS inits a process knwon as parameter binding, in which it matches each object into the command to the various parameters specified by the command's creator.
To see if a pipeline is possible you must use Full parameter on the Get-Help command.
By default you cannot run a script. You then need to customize the Execution Policies, just put it on Unrestricted -> Allows to run any script.
you also have:
- Restricted: does not allow running scripts
- AllSigned: scripts need to be signed
- RemoteSigned: run scripts you made or script downloaded that where signed by others
A script signature is an encrypted string at the end of your script
extension is .ps1.
-
eq
-
ne
-
gt
-
ge
-
lt
-
le
-
contains
-
not
-
Quiet -> gives the essential information of a method
if () {} else if
Hier ga je alle condities controleren (niet als je een break gebruikt), bij else if else if else if niet.
- foreach, for, do/while, ...
.... We already know this stuff..
for each is under the hood the best way
for ($i = 0 ; $i lt 10 ; $i ++)
two types of errors:
-
Terminating error: any error that stopt execution of the code
-
Non-terminating error: error that is not servere enough to halt the rest of the code
Decision is made by the developer!
some information...
try catch block
catch does not catch non-terminating errors
each error is stored in a PS automatic variable $Error
Code can be broken down into functions
- Similar to cmdlets such as Start-Service and Write-Host
function {code_block}
use the Noun-Verb name
This can force you to use the parameter
default parameters and parameter validation also exist
ValidateSet can be used to validate the parameters, @{'1', '2'} -> Only Version 1 and 2 are applicable
- ByValue (entire object)
- ByPropertyName (single object property)
You need to however add a process block, because otherwise it will only do it on the last iteration
Importing
...
-
.psm1 file extensionm can be a PS module
-
Module manifest '.psd1' file
There is a Powershell Gallery to view all modules
This can be done using PSSessions
essentially lamda functions
when we define a var to a codeblock, we need to add & before the var to execute the codeblock
Invoke-Command:
two way to use it:
- first is when running ad hoc commands
- second is using interactive sessions
You can also use Invoke-Command to execute entire scripts
best way is to use $using before any local variable name
...
use Enter-PSSession
Powershell is really good to parse CSV files
Essentially, this import tool is really strong
You can also create CSV files
Same with JSON, ConvertFrom-JSON, takes raw JSON and converts it to a PS object.
You can also just create JSON
This is also truly possible, wow! 😎
This is just a keyvalue store (manage printers, servers, ...)
ActiveDirectory module is required!
- Search-ADAccount command:
Sometimes, when you know which AD object your looking for, you don't need a filter
...
Each user can be part of a group
whatever
Labs are mandatory
V, VI, VII are dangerous, because the clusters could get destroyed, make sure to be proactive







