-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Functions Cmdlets Modules and the Pipeline.ps1
148 lines (113 loc) · 5.26 KB
/
2_Functions Cmdlets Modules and the Pipeline.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Touch on Git and Github
# Forgot to say what PowerShell was actually designed for
# Since PoSh 3.0 CmdLets from Modules NOT actively loaded in
# session still show in autocomplete and auto load the module
# if you use the command. Modules are still session based.
# Can prove using Get-Module
# Weekend Practice coming on Fridays
# -------------------------------------------------------
# Commenting your Code
# -------------------------------------------------------
# Comment
# -------------------------------------------------------
# Rubber Duck Debugging
# -------------------------------------------------------
# -------------------------------------------------------
# Functions
# -------------------------------------------------------
# Functions are reusable blocks of code that perform a
# specific purpose or action. If you find yourself writing
# the same code again then it should probably be a function!
Function Get-NextMonth {
return (Get-Date).AddMonths(1)
}
$newDate = Get-NextMonth
# Once a Function is ran in the PowerShell session it can be
# be reused over and over again
# Try typing 'Get-NextMonth' in PowerShell window
# -------------------------------------------------------
# CmdLets
# -------------------------------------------------------
# CmdLets are reusable blocks of code, these are the main
# building blocks of PowerShell. Get-Date is a CmdLet.
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-5.1
# CmdLets and Functions seem very similar on the surface but
# CmdLets provide additional argument support such as -WhatIf
# and -Verbose (there are others)
#
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-5.1
# -- Common Params --
# -Verbose
# -ErrorAction
# -WhatIf
# -- Other Params --
# -Debug
# -WarningAction
# -InformationAction
# -ErrorVariable
# -WarningVariable
# -InformationVariable
# -OutVariable
# -OutBuffer
# -PipelineVariable
# To make things confusing, any Function can be turned in to
# a CmdLet by adding CmdletBinding
Function Get-NextMonth {
[CmdletBinding()] # <- CmdLetBinding Attribute
Param() # <- Parameters Attribute
Write-Verbose -Message "Getting Next Month..." # <- Write Verbose will only output to screen if -Verbose is passed
return (Get-Date).AddMonths(1)
}
Function Get-NextMonth {
[CmdletBinding()] # <- CmdLetBinding Attribute
Param(
[Parameter(Mandatory = $true)]
[Int]$Months
) # <- Parameters Attribute
Write-Verbose -Message "Getting $($Months) Months..." # <- Write Verbose will only output to screen if -Verbose is passed
Write-Debug -Message "This is a debug message" # <- Write-Debug will only output to screen if -Debug is passed
return (Get-Date).AddMonths($Months)
}
# -------------------------------------------------------
# Modules
# -------------------------------------------------------
# Modules are a group of CmdLets/Functions that are generally
# geared to a specific purpose. For example 'AzureAD' contains
# all the Azure AD CmdLets needed to interact with Microsoft
# Azure.
# Those with Programming background will compare this to a Library or
# Package. (C# think DLL)
# Modules are imported using the CmdLet 'Import-Module', Import-Module
# loads every CmdLet and Function of the Module in to the session.
# As we saw in the Functions topic once something is in the session
# you can use them over and over again.
Import-Module -Name AzureAD
Remove-Module -Name AzureAD
# To see what commands are available after importing a Module use
Get-Command -Module AzureAD
# -------------------------------------------------------
# Finding Commands
# -------------------------------------------------------
Get-Command | Where-Object {$_.Name -like "*EventLog*"}
# -------------------------------------------------------
# Reading the in terminal Help/Microsoft Help Webpage
# -------------------------------------------------------
# How to read the help docs, param sets
# Show ActiveDirectory Module on my MGMT01
# -------------------------------------------------------
# PowerShell Pipeline
# -------------------------------------------------------
# In PowerShell you take small peices and build upon them,
# or operate on them in different steps. PowerShell lets you
# pass objects through the pipeline to the next operation.
# Think car assembly line, stop 1 some nuts get welded on, stop 2
# some more nuts get welded on and this continues until you
# have a finished car.
# Items are passed through the pipeline using the pipe character '|'
Get-Date | Select-Object Month
$today = Get-Date
# The results of Get-Date are pushed through the Pipeline to Select-Object
# Select-Object selects the Property 'Month' and pushes it to the pipeline,
# and since there is no more pipe to push through it is returned to the terminal.
# Tomas
# https://www.trendmicro.com/vinfo/us/security/news/cybercrime-and-digital-threats/locked-loaded-and-in-the-wrong-hands-legitimate-tools-weaponized-for-ransomware-in-2021