-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_Conditionals.ps1
185 lines (141 loc) · 3.87 KB
/
5_Conditionals.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -------------------------------------------------------
# Conditionals
# -------------------------------------------------------
# Conditionals evaluate to true or false.
# If/Else/ElseIf are conditionals
# Operators
# -eq, Equals
# -ne, Not Equals
# -gt, Greater Than
# -ge, Greater Than or Equal To
# -lt, Less Than
# -le, Less Than or Equal To
# -like, Like
# -notlike, Not Like
# Conditionals consist of the 'If' statement followed by comparison
# which uses operators above (or not) and then the code you want to run
# if the conditional evaluates to true
$sample = 1
if($sample -eq 1) {
Write-Output "Sample is Equal to 1"
}
if($sample -gt 1) {
Write-Output "Sample is Greater Than 1"
}
if($sample -ge 1) {
Write-Output "Sample is Greater Than or Equal to 1"
}
if($sample -like 1) {
Write-Output "Sample is Like 1"
}
if($sample -notlike 1) {
Write-Output "Sample is Not Like 1"
}
# -------------------------------------------------------
# Else
# -------------------------------------------------------
# Else lets you run additional code if the conditional didn't
# evaluate to true
$sample = 2
if($sample -eq 1) {
Write-Output "Sample is Equal to 1"
} else {
Write-Output "Sample does not Equal 1"
}
# The logic can be reversed
if($sample -ne 1) {
Write-Output "Sample does not Equal 1"
} else {
Write-Output "Sample is Equal to 1"
}
# -------------------------------------------------------
# ElseIf
# -------------------------------------------------------
# ElseIf lets you test for additional conditionals before
# processing final else.
# You can use as many elseifs as you want but can make code
# harder to read
$sample = 3
$sample = 2
$sample = 8
if($sample -eq 3) {
Write-Output "Sample is Equal to 3"
} elseif($sample -eq 2) {
Write-Output "Sample is Equal to 2"
} else {
Write-Output "Sample is something else"
}
# -------------------------------------------------------
# Or Not?!?
# -------------------------------------------------------
# If you have a variable holding a boolean or function that
# returns a boolean then you can do if() without comparison
# operator
Function Test-Me {
return $true
}
if(Test-Path -Path "C:\Temp") {
Write-Output "Path Exists"
} else {
Write-Output "Path does not exist"
}
$myBool = $false
if(!$myBool) {
Write-Output "Value is True"
} else {
Write-Output "Value is False"
}
# -------------------------------------------------------
# Testing
# -------------------------------------------------------
# You can test by omitting the IF and just typing
# the ($sample -eq 1)
!($sample -eq 1)
# PowerShell will spit out True or False
# -------------------------------------------------------
# -AND -Or
# -------------------------------------------------------
$sample = 2
if(($sample -gt 4 -or $sample -lt 10) -and ($sample -gt 1)) {
Write-Output "Sample is good!"
}
T T = T
AND(T T T T T T F) = F
OR(T F T) = T
$colors = "Red", "Blue", "Green"
if($colors -like "*Blue*") {
Write-Output "Didn't find it"
}
$today = Get-Date
$dates = New-Object -TypeName System.Collections.Generic.List[PSObject]
$dates.Add((Get-Date))
$dates.Add($today)
if($dates -like $today) {
Write-Output "Found today"
}
$logs = Get-EventLog -LogName System -Newest 5
if($logs.Message -like "*DCOM*") {
Write-Output "Found"
}
$today.ToString()
$age = 41
$age.ToString()
$hash = @{
one = "one"
two = "two"
}
$hash.ToString()
$colors.ToString()
# -------------------------------------------------------
# Switch
# -------------------------------------------------------
$value = "bob"
switch($value) {
"tim" {
}
"bob" {
Test-Me
}
"charlie" {"three"}
default {"default"}
}