WEBVTT

1
00:00:00.000 --> 00:00:01.380
<v Instructor>In this lesson,</v>

2
00:00:01.380 --> 00:00:03.690
we will learn about PowerShell.

3
00:00:03.690 --> 00:00:06.660
PowerShell is a powerful scripting language

4
00:00:06.660 --> 00:00:10.800
and command line shell designed for automating tasks

5
00:00:10.800 --> 00:00:14.970
and managing configurations in Windows environments.

6
00:00:14.970 --> 00:00:19.530
In PowerShell, data structures such as variables, arrays,

7
00:00:19.530 --> 00:00:24.420
hash tables, strings, and objects are used to store

8
00:00:24.420 --> 00:00:26.730
and manipulate complex data,

9
00:00:26.730 --> 00:00:29.880
making it easy to handle system information

10
00:00:29.880 --> 00:00:32.190
and configuration details.

11
00:00:32.190 --> 00:00:35.310
Script control structures, including loops

12
00:00:35.310 --> 00:00:38.430
like for, foreach, and while,

13
00:00:38.430 --> 00:00:42.330
and conditional statements like, if and switch,

14
00:00:42.330 --> 00:00:45.990
allows scripts to execute repetitive tasks

15
00:00:45.990 --> 00:00:49.860
and make decisions based on specific conditions.

16
00:00:49.860 --> 00:00:53.490
Let's learn more about PowerShell data structures

17
00:00:53.490 --> 00:00:55.560
and control structures.

18
00:00:55.560 --> 00:00:58.860
First, we have PowerShell data structures.

19
00:00:58.860 --> 00:01:02.550
In PowerShell, data structures play a key role

20
00:01:02.550 --> 00:01:05.550
in managing and organizing data,

21
00:01:05.550 --> 00:01:10.550
variables, arrays, hash tables and objects are all tools

22
00:01:10.950 --> 00:01:14.340
that make it easy to store and manipulate data

23
00:01:14.340 --> 00:01:16.410
with PowerShell scripts.

24
00:01:16.410 --> 00:01:19.620
For example, in a PowerShell script,

25
00:01:19.620 --> 00:01:23.400
a variable could store something like a log file path

26
00:01:23.400 --> 00:01:27.030
or even specific log on failure details.

27
00:01:27.030 --> 00:01:29.610
Overall, PowerShell allows us

28
00:01:29.610 --> 00:01:33.043
to manage data efficiently through cmdlets.

29
00:01:33.043 --> 00:01:38.043
A cmdlet is a lightweight verb-noun formatted command

30
00:01:38.070 --> 00:01:39.720
that performs an action,

31
00:01:39.720 --> 00:01:44.190
and typically returns a microsoft.net object.

32
00:01:44.190 --> 00:01:47.970
Now let's take a look at some PowerShell code.

33
00:01:47.970 --> 00:01:52.890
In this case, the output of the Get-EventLog cmdlet

34
00:01:52.890 --> 00:01:57.540
is a collection of objects that represent log entries,

35
00:01:57.540 --> 00:01:59.970
with each one containing properties

36
00:01:59.970 --> 00:02:02.610
like timewritten and message.

37
00:02:02.610 --> 00:02:05.850
These are microsoft.net objects,

38
00:02:05.850 --> 00:02:09.030
and they act as a powerful data structure

39
00:02:09.030 --> 00:02:12.330
allowing us to easily access and process

40
00:02:12.330 --> 00:02:14.700
specific pieces of data.

41
00:02:14.700 --> 00:02:17.460
In this snippet, PowerShell is retrieving

42
00:02:17.460 --> 00:02:22.140
the five most recent log entries from the security log

43
00:02:22.140 --> 00:02:26.580
that match the instance ID of 4625,

44
00:02:26.580 --> 00:02:30.120
which corresponds to log-on failures.

45
00:02:30.120 --> 00:02:32.940
The data is output as objects

46
00:02:32.940 --> 00:02:35.700
and sent through the pipe operator,

47
00:02:35.700 --> 00:02:38.910
which is represented by that vertical line.

48
00:02:38.910 --> 00:02:42.540
A pipe operator takes the output from the command

49
00:02:42.540 --> 00:02:45.510
on its left and sends it as input

50
00:02:45.510 --> 00:02:48.180
to the command on its right.

51
00:02:48.180 --> 00:02:51.780
The properties timewritten and messages

52
00:02:51.780 --> 00:02:54.180
are selected from the log entries

53
00:02:54.180 --> 00:02:57.090
on the left side of the pipe operator,

54
00:02:57.090 --> 00:03:02.090
and they are passed as input into the out file cmdlet,

55
00:03:02.310 --> 00:03:07.310
which writes this information to a file called log-fail.txt.

56
00:03:08.580 --> 00:03:12.960
This demonstrates how PowerShell handles complex data

57
00:03:12.960 --> 00:03:17.340
by turning log entries into objects that can be filtered,

58
00:03:17.340 --> 00:03:21.000
manipulated, and stored with minimal effort.

59
00:03:21.000 --> 00:03:24.600
PowerShell also uses other data structures,

60
00:03:24.600 --> 00:03:28.440
such as arrays, to store lists of items.

61
00:03:28.440 --> 00:03:31.320
For example, an array could be used

62
00:03:31.320 --> 00:03:33.690
to store multiple log entries

63
00:03:33.690 --> 00:03:37.320
or a list of user accounts for auditing.

64
00:03:37.320 --> 00:03:39.780
Arrays can be looped through easily,

65
00:03:39.780 --> 00:03:43.560
as we'll see when discussing control structures,

66
00:03:43.560 --> 00:03:45.990
making them a flexible tool

67
00:03:45.990 --> 00:03:48.840
for managing collections of data.

68
00:03:48.840 --> 00:03:53.460
Overall, Powershell's built-in support for handling objects

69
00:03:53.460 --> 00:03:57.780
and structured data makes it particularly well-suited

70
00:03:57.780 --> 00:04:00.900
for system administration tasks.

71
00:04:00.900 --> 00:04:04.980
Second, we have PowerShell control structures.

72
00:04:04.980 --> 00:04:08.610
Control structures in PowerShell enables scripts

73
00:04:08.610 --> 00:04:12.060
to handle repetitive tasks, make decisions,

74
00:04:12.060 --> 00:04:15.900
and respond dynamically to different conditions.

75
00:04:15.900 --> 00:04:20.670
These structures allow for efficient automation of tasks,

76
00:04:20.670 --> 00:04:23.280
such as gathering system data,

77
00:04:23.280 --> 00:04:26.460
iterating over collections of information,

78
00:04:26.460 --> 00:04:30.600
and performing actions based on specific criteria.

79
00:04:30.600 --> 00:04:34.500
For example, when using the right host cmdlet,

80
00:04:34.500 --> 00:04:37.080
a message can be printed to the screen

81
00:04:37.080 --> 00:04:41.310
to inform the user about the progress of the script.

82
00:04:41.310 --> 00:04:45.900
This feedback is used in scripts that may take time to run,

83
00:04:45.900 --> 00:04:50.490
or involve multiple steps, ensuring the user understands

84
00:04:50.490 --> 00:04:53.640
what is being executed as it occurs.

85
00:04:53.640 --> 00:04:57.840
Next, the if and else control structures are used

86
00:04:57.840 --> 00:05:00.960
for decision making, allowing a script

87
00:05:00.960 --> 00:05:04.440
to take different actions based on whether a condition

88
00:05:04.440 --> 00:05:06.360
is true or false.

89
00:05:06.360 --> 00:05:09.180
For instance, a script might check

90
00:05:09.180 --> 00:05:11.520
whether a user is an administrator,

91
00:05:11.520 --> 00:05:14.040
and display a message accordingly.

92
00:05:14.040 --> 00:05:19.040
Additionally, loops like for and foreach make it possible

93
00:05:19.110 --> 00:05:22.770
to repeat tasks over a series of elements,

94
00:05:22.770 --> 00:05:26.760
such as a long list of log entries or users.

95
00:05:26.760 --> 00:05:28.980
The loops work hand in hand

96
00:05:28.980 --> 00:05:31.740
with cmdlets like a Get-EventLog,

97
00:05:31.740 --> 00:05:34.680
allowing the script to dynamically retrieve

98
00:05:34.680 --> 00:05:38.430
and process system data, such as checking for

99
00:05:38.430 --> 00:05:42.330
log-on failures or specific event IDs.

100
00:05:42.330 --> 00:05:46.650
Next, using loops to iterate over collections of data

101
00:05:46.650 --> 00:05:50.280
allows PowerShell scripts to automate tasks

102
00:05:50.280 --> 00:05:54.480
that would otherwise be time-consuming or prone to error

103
00:05:54.480 --> 00:05:55.980
if done manually.

104
00:05:55.980 --> 00:05:59.280
For example, a foreach loop can go through

105
00:05:59.280 --> 00:06:01.950
all the log entries in a collection,

106
00:06:01.950 --> 00:06:04.980
checking each one for a specific condition,

107
00:06:04.980 --> 00:06:07.110
like being a failed log-on.

108
00:06:07.110 --> 00:06:09.180
If a failure is found,

109
00:06:09.180 --> 00:06:11.790
the script can take appropriate action,

110
00:06:11.790 --> 00:06:14.880
such as printing out a message to the screen,

111
00:06:14.880 --> 00:06:18.960
or saving those details to a file for further review.

112
00:06:18.960 --> 00:06:21.960
This automated decision-making process

113
00:06:21.960 --> 00:06:24.750
makes PowerShell scripts highly flexible

114
00:06:24.750 --> 00:06:27.300
and powerful for system management.

115
00:06:27.300 --> 00:06:29.880
Finally, we have a switch statement.

116
00:06:29.880 --> 00:06:33.600
This is another important control structure in PowerShell.

117
00:06:33.600 --> 00:06:36.090
It provides a more efficient way

118
00:06:36.090 --> 00:06:38.130
to handle multiple conditions

119
00:06:38.130 --> 00:06:41.340
compared to using multiple if statements.

120
00:06:41.340 --> 00:06:45.090
When a variable can have several possible values,

121
00:06:45.090 --> 00:06:47.490
a switch statement allows the script

122
00:06:47.490 --> 00:06:50.850
to match the variable against each value,

123
00:06:50.850 --> 00:06:54.450
and then execute the appropriate block of code.

124
00:06:54.450 --> 00:06:57.540
By combining conditional statements, loops,

125
00:06:57.540 --> 00:07:01.200
and cmdlets, PowerShell control structures provide

126
00:07:01.200 --> 00:07:04.590
the tools needed to automate and simplify

127
00:07:04.590 --> 00:07:06.930
complex system tasks.

128
00:07:06.930 --> 00:07:09.420
Now let's take a look at a script,

129
00:07:09.420 --> 00:07:11.640
and I know this is a long one.

130
00:07:11.640 --> 00:07:15.900
The script on the screen uses PowerShell control structures

131
00:07:15.900 --> 00:07:20.790
like if, else, for, foreach, and switch

132
00:07:20.790 --> 00:07:25.230
to automate tasks, handle data, and make decisions

133
00:07:25.230 --> 00:07:27.540
based on different conditions.

134
00:07:27.540 --> 00:07:30.750
Let's take a look at each control structure.

135
00:07:30.750 --> 00:07:34.320
The script first checks if the logged in user

136
00:07:34.320 --> 00:07:38.280
is an administrator using the if else statement.

137
00:07:38.280 --> 00:07:42.450
If the user is admin, a welcome message is printed.

138
00:07:42.450 --> 00:07:46.740
Otherwise it displays an access denied message.

139
00:07:46.740 --> 00:07:49.020
Next, a for loop is used

140
00:07:49.020 --> 00:07:51.690
to iterate through a series of numbers,

141
00:07:51.690 --> 00:07:55.620
printing the number of the iteration each time.

142
00:07:55.620 --> 00:08:00.420
This is a simple but repetitive task often used in scripts.

143
00:08:00.420 --> 00:08:04.260
Next, in the script, a foreach loop iterates

144
00:08:04.260 --> 00:08:08.250
through a list of users, printing out each user's name

145
00:08:08.250 --> 00:08:10.080
as it processes them.

146
00:08:10.080 --> 00:08:13.530
This is useful when working with collections, like arrays

147
00:08:13.530 --> 00:08:15.720
or lists in PowerShell.

148
00:08:15.720 --> 00:08:17.970
And finally, a switch statement

149
00:08:17.970 --> 00:08:20.910
checks the value of the role variable,

150
00:08:20.910 --> 00:08:24.690
and performs actions based on the match to value.

151
00:08:24.690 --> 00:08:27.780
For example, if the role is admin,

152
00:08:27.780 --> 00:08:30.540
the user gets admin privileges.

153
00:08:30.540 --> 00:08:35.280
Or if the role is user or guest, appropriate messages

154
00:08:35.280 --> 00:08:37.530
are printed out to the screen.

155
00:08:37.530 --> 00:08:40.710
As you can see, PowerShell control structures,

156
00:08:40.710 --> 00:08:45.710
like if, else, for, foreach, and switch provide tools

157
00:08:46.080 --> 00:08:51.080
for automating complex tasks and handling data efficiently.

158
00:08:51.120 --> 00:08:53.580
By combining these control structures

159
00:08:53.580 --> 00:08:56.640
with cmdlets, scripts can make decisions,

160
00:08:56.640 --> 00:08:58.650
perform repetitive actions

161
00:08:58.650 --> 00:09:02.190
and respond dynamically to various conditions,

162
00:09:02.190 --> 00:09:06.330
making PowerShell highly effective for system management.

163
00:09:06.330 --> 00:09:11.330
So remember, PowerShell is a powerful scripting language

164
00:09:11.760 --> 00:09:16.320
designed for automating tasks and managing configurations

165
00:09:16.320 --> 00:09:18.390
in a Windows environment.

166
00:09:18.390 --> 00:09:22.650
It uses various data structures, such as variables,

167
00:09:22.650 --> 00:09:25.770
arrays, hash tables, and objects

168
00:09:25.770 --> 00:09:30.000
to efficiently store and manipulate complex data.

169
00:09:30.000 --> 00:09:34.530
Control structures like if, else, for, foreach,

170
00:09:34.530 --> 00:09:37.740
and switch enable PowerShell scripts

171
00:09:37.740 --> 00:09:39.990
to perform repetitive tasks

172
00:09:39.990 --> 00:09:44.010
and make decisions based on specific conditions.

173
00:09:44.010 --> 00:09:47.760
By combining these control structures with cmdlets,

174
00:09:47.760 --> 00:09:50.640
PowerShell scripts can dynamically retrieve,

175
00:09:50.640 --> 00:09:53.910
process, and act upon system data.

176
00:09:53.910 --> 00:09:58.110
This flexibility and functionality make PowerShell

177
00:09:58.110 --> 00:10:01.770
an effective tool for automating and managing

178
00:10:01.770 --> 00:10:04.503
complex system tasks.

