1
00:00:00,040 --> 00:00:02,590
So let's go back into the PowerShell console,

2
00:00:02,590 --> 00:00:06,040
and we'll create a function within a PowerShell script.

3
00:00:06,040 --> 00:00:10,380
We'll then make another function with parameters within the PowerShell script.

4
00:00:10,380 --> 00:00:13,780
And then we'll look at how we tie it together and create the basic script.

5
00:00:13,780 --> 00:00:16,530
And then, of course, we'll also execute those as well.

6
00:00:16,530 --> 00:00:20,120
Okay, so after all the things that we've gone through,

7
00:00:20,120 --> 00:00:23,220
the most important thing now is to look at how we construct these

8
00:00:23,220 --> 00:00:26,630
into a script of some description and actually start to use

9
00:00:26,630 --> 00:00:28,290
functions the way that they are intended.

10
00:00:28,290 --> 00:00:32,870
Now, of course, one of the ones that we looked at earlier was this approach here,

11
00:00:32,870 --> 00:00:37,500
the ability for us to use a parameter and then obviously pass various

12
00:00:37,500 --> 00:00:40,670
things to it in order for that function to actually execute.

13
00:00:40,670 --> 00:00:43,550
Now what I've got here are a couple of $outputPaths and $paths,

14
00:00:43,550 --> 00:00:45,870
so I'm actually going to right‑click these variables, Run

15
00:00:45,870 --> 00:00:48,800
Selection, and that will just make sure those are there.

16
00:00:48,800 --> 00:00:51,870
And then I'm just going to kind of scroll that down a little bit here

17
00:00:51,870 --> 00:00:55,440
so it's out of the way. Now what I want to do is kind of talk about

18
00:00:55,440 --> 00:01:01,480
some different ways that we can kind of create functions and classes

19
00:01:01,480 --> 00:01:03,740
and things that we can execute.

20
00:01:03,740 --> 00:01:04,250
Now, of course,

21
00:01:04,250 --> 00:01:09,970
the first thing here is to look at how we get interaction with individual users.

22
00:01:09,970 --> 00:01:12,560
So what I'm going to do is leave the PowerShell kind of

23
00:01:12,560 --> 00:01:14,710
Visual Studio Code open for a second,

24
00:01:14,710 --> 00:01:17,850
and I'm going to launch Windows Terminal again, so as administrator,

25
00:01:17,850 --> 00:01:22,560
run that. Wait for that to load and just clear that one.

26
00:01:22,560 --> 00:01:25,940
And then, of course, I'm going to make sure that we're using PowerShell 7.

27
00:01:25,940 --> 00:01:27,960
So let's just clear that message here.

28
00:01:27,960 --> 00:01:29,440
And then we're good to go.

29
00:01:29,440 --> 00:01:34,540
And what I'm going to do is just paste a function directly into this one.

30
00:01:34,540 --> 00:01:38,500
And what you can see is the function is called Get‑Answer. I actually have

31
00:01:38,500 --> 00:01:41,670
some funny characters so I'll probably have to do that again in a second. But,

32
00:01:41,670 --> 00:01:45,380
basically, there's a question called Read‑Host "What is the Capital City of

33
00:01:45,380 --> 00:01:50,050
Australia?" And then we're combining it with an if statement and some colors

34
00:01:50,050 --> 00:01:51,740
and a few other bits and pieces.

35
00:01:51,740 --> 00:01:56,580
So let me just clear that, and I'll just remove the extra characters.

36
00:01:56,580 --> 00:02:02,310
That is an important thing to kind of remember is that often when you're copying

37
00:02:02,310 --> 00:02:05,910
and pasting things around from other kinds of applications,

38
00:02:05,910 --> 00:02:10,040
you may end up with extra characters that don't really make any sense.

39
00:02:10,040 --> 00:02:11,060
Okay, so we have them in.

40
00:02:11,060 --> 00:02:13,400
So what I'll do now is just enter that.

41
00:02:13,400 --> 00:02:16,050
So we're thinking about building a PowerShell script.

42
00:02:16,050 --> 00:02:22,670
We want some user interaction, so I'm going to say Get‑Answer and just Enter.

43
00:02:22,670 --> 00:02:27,130
And, of course, instantly it provides the user the ability to type something.

44
00:02:27,130 --> 00:02:32,150
So I'm going to say Sydney and Enter, and it will say,

45
00:02:32,150 --> 00:02:36,210
Incorrect!! You entered (the value you typed) which is incorrect,

46
00:02:36,210 --> 00:02:39,000
please try again. So I'm going to try again.

47
00:02:39,000 --> 00:02:43,490
And, of course, as everybody knows, the actual capital is Canberra.

48
00:02:43,490 --> 00:02:48,140
And of course, it says, Correct!! You entered (the value). So a simple approach.

49
00:02:48,140 --> 00:02:51,090
But you can have a PowerShell script that literally uses

50
00:02:51,090 --> 00:02:53,710
what's called, if I just select this here,

51
00:02:53,710 --> 00:02:59,540
Read‑Host and will ask the end user for some kind of interaction.

52
00:02:59,540 --> 00:03:02,090
Now that's a great way of doing it.

53
00:03:02,090 --> 00:03:06,240
But what about if we wanted to be able to pass those values

54
00:03:06,240 --> 00:03:08,650
maybe as part of the function instead?

55
00:03:08,650 --> 00:03:13,040
So I'm now just going to copy and paste another one in.

56
00:03:13,040 --> 00:03:14,240
So here it is.

57
00:03:14,240 --> 00:03:16,910
It's the same principle again, so get the answer.

58
00:03:16,910 --> 00:03:21,570
And this time it will say $question = Read‑Host "Hi (something).

59
00:03:21,570 --> 00:03:24,910
What is the Capital City in Australia?" And then,

60
00:03:24,910 --> 00:03:26,440
of course, it has the same thing.

61
00:03:26,440 --> 00:03:29,510
Now notice we're using something different, and we used this previously.

62
00:03:29,510 --> 00:03:32,830
So we're using what's called arguments, though, in this instance,

63
00:03:32,830 --> 00:03:36,680
we're expecting the end user to type something. So I can say

64
00:03:36,680 --> 00:03:43,210
Get‑Answer "Liam Cleary" and Enter. And it will say, Hi Liam

65
00:03:43,210 --> 00:03:47,530
Cleary, what is the Capital City in Australia? And then because

66
00:03:47,530 --> 00:03:51,010
I already know the answer, I can say Canberra, and it gives me the value.

67
00:03:51,010 --> 00:03:53,200
So, once again, a different way,

68
00:03:53,200 --> 00:03:57,250
it's still utilizing the Read‑Host option for the end user to provide

69
00:03:57,250 --> 00:03:59,900
information or the admin to provide information.

70
00:03:59,900 --> 00:04:03,240
However, what it's doing is making it more personal.

71
00:04:03,240 --> 00:04:06,410
Now, of course, we know that we could go a little step further here,

72
00:04:06,410 --> 00:04:13,240
and we could change that from using an argument to actually using a variable.

73
00:04:13,240 --> 00:04:16,770
So you can see here we've got the same kind of function again,

74
00:04:16,770 --> 00:04:20,440
so we're going to say Get‑Answer.

75
00:04:20,440 --> 00:04:23,050
And this time it requires a name.

76
00:04:23,050 --> 00:04:27,430
So I'm going to say in quotes (can't even spell my own

77
00:04:27,430 --> 00:04:31,490
name) "Liam Cleary" Enter. And it will say,

78
00:04:31,490 --> 00:04:32,110
Hi, Liam.

79
00:04:32,110 --> 00:04:35,040
What's the Capital City?

80
00:04:35,040 --> 00:04:37,140
Canberra, and correct.

81
00:04:37,140 --> 00:04:38,400
So a couple different ways.

82
00:04:38,400 --> 00:04:39,640
We used arguments.

83
00:04:39,640 --> 00:04:41,330
We used Read‑Host.

84
00:04:41,330 --> 00:04:43,250
We used a combination of those two.

85
00:04:43,250 --> 00:04:46,440
Plus, we changed it to use a variable.

86
00:04:46,440 --> 00:04:48,780
Now what about if we want to go a little step further?

87
00:04:48,780 --> 00:04:53,030
We want to be able to provide a series of values so that

88
00:04:53,030 --> 00:04:55,180
the end user can just kind of pick.

89
00:04:55,180 --> 00:04:56,470
Now that would make more sense.

90
00:04:56,470 --> 00:05:01,640
Now that helps us by restricting it to specific values.

91
00:05:01,640 --> 00:05:04,200
It stops people spelling things incorrectly.

92
00:05:04,200 --> 00:05:06,690
And it also means that from a code perspective,

93
00:05:06,690 --> 00:05:10,540
we already know what the answers are that we should be expecting.

94
00:05:10,540 --> 00:05:14,140
So I'm now just going to copy and paste another one in.

95
00:05:14,140 --> 00:05:14,970
So here we are.

96
00:05:14,970 --> 00:05:16,210
This is a new function.

97
00:05:16,210 --> 00:05:21,140
It's called Test‑WhatIsCapitalCityofAustralia. So we've removed the question.

98
00:05:21,140 --> 00:05:23,970
We've put that into the function. And then you'll see we've got

99
00:05:23,970 --> 00:05:28,830
this Param section here where we've said go and get the parameter,

100
00:05:28,830 --> 00:05:32,850
so provide one as a mandatory one called $city. And then we have

101
00:05:32,850 --> 00:05:37,020
what's called ValidateSet, which provides four specific options.

102
00:05:37,020 --> 00:05:40,270
So what does that look like for the person executing this?

103
00:05:40,270 --> 00:05:46,040
So I'm going to say Test‑WhatIsCapitalCityofAustralia. Then I

104
00:05:46,040 --> 00:05:48,410
can do a tab here, and you'll see ‑city.

105
00:05:48,410 --> 00:05:53,120
And then when I tab, it lets me loop through the various options.

106
00:05:53,120 --> 00:05:56,190
So if I choose Melbourne, it's going to say Incorrect.

107
00:05:56,190 --> 00:05:59,930
If I go back here, try Canberra, I get the correct value.

108
00:05:59,930 --> 00:06:06,830
So, again, we change the input of the script to then have a predetermined

109
00:06:06,830 --> 00:06:11,740
list of values so that it makes life a little bit easier.

110
00:06:11,740 --> 00:06:15,850
Now, of course, there are lots of other ways of interacting with scripts.

111
00:06:15,850 --> 00:06:19,140
So let's go back to Visual Studio Code,

112
00:06:19,140 --> 00:06:23,580
and what we can see here is I've got some other options available to us. So you

113
00:06:23,580 --> 00:06:27,210
can see I'm creating what's called an Autocomplete option,

114
00:06:27,210 --> 00:06:28,970
which contains all the values.

115
00:06:28,970 --> 00:06:32,610
Now what I wanted to do here is I wanted to use the same

116
00:06:32,610 --> 00:06:35,110
mechanism that we had before where we're saying, What's the

117
00:06:35,110 --> 00:06:37,650
Capital City, and providing some values.

118
00:06:37,650 --> 00:06:40,540
But I wanted those values to come from somewhere else.

119
00:06:40,540 --> 00:06:46,250
So maybe from an array or maybe a JSON file or maybe an XML.

120
00:06:46,250 --> 00:06:50,140
So what we can do, I'm going to slide this over here, is you

121
00:06:50,140 --> 00:06:52,330
can see we can create what's called a class.

122
00:06:52,330 --> 00:06:54,720
So inside the PowerShell script,

123
00:06:54,720 --> 00:06:58,640
we create a class for the definition of the content.

124
00:06:58,640 --> 00:07:04,380
So, for example, this one is an IValidateSetValuesGenerator.

125
00:07:04,380 --> 00:07:09,450
What that means is it's going to create a list of values, and it can be dynamic.

126
00:07:09,450 --> 00:07:13,780
So at the moment I have static values typed into an array.

127
00:07:13,780 --> 00:07:17,250
But that could be a JSON call, an XML call, a web

128
00:07:17,250 --> 00:07:19,540
service call, whatever it would be.

129
00:07:19,540 --> 00:07:20,980
So I have this class.

130
00:07:20,980 --> 00:07:24,590
I've defined it as a string called GetValidValues. And

131
00:07:24,590 --> 00:07:26,570
then I can return a city object.

132
00:07:26,570 --> 00:07:29,720
So I'm going to execute that command. That will then

133
00:07:29,720 --> 00:07:33,340
populate here ready for me to use it.

134
00:07:33,340 --> 00:07:38,140
So this $Cities option should be valid now for me to use.

135
00:07:38,140 --> 00:07:41,750
Now what we can do is like we did with the Australia one, I

136
00:07:41,750 --> 00:07:44,300
want to somehow test the population of Hawaii.

137
00:07:44,300 --> 00:07:48,630
So I've created a function, and you can see how Visual Studio Code is

138
00:07:48,630 --> 00:07:52,240
great at rendering this, color coding the whole thing.

139
00:07:52,240 --> 00:07:53,520
So what I've done here,

140
00:07:53,520 --> 00:07:57,710
the different line is this one where the ValidateSet instead of

141
00:07:57,710 --> 00:08:00,440
it being an array of values that we type,

142
00:08:00,440 --> 00:08:04,920
it's actually calling into that specific class called Cities,

143
00:08:04,920 --> 00:08:06,440
which is listed here.

144
00:08:06,440 --> 00:08:13,040
So if I was to go ahead and just select this function and then go Run Selection,

145
00:08:13,040 --> 00:08:14,740
so now we have it.

146
00:08:14,740 --> 00:08:22,290
Now I can say Test‑PopulationOfHawaii, and then it finds the property ‑Answer,

147
00:08:22,290 --> 00:08:23,340
which is the variable.

148
00:08:23,340 --> 00:08:24,970
And then if I just tab,

149
00:08:24,970 --> 00:08:32,110
we're getting numbers now. If I just go back here, 1.4, 2 million, 500 Thousand,

150
00:08:32,110 --> 00:08:34,790
750 Thousand. So no specific order.

151
00:08:34,790 --> 00:08:36,930
But it means that I can go through and select.

152
00:08:36,930 --> 00:08:40,340
If I choose Enter, it tells me it's incorrect.

153
00:08:40,340 --> 00:08:43,330
If I go back here, say 1.4, it tells me it's correct.

154
00:08:43,330 --> 00:08:45,470
So very similar to what we did before,

155
00:08:45,470 --> 00:08:49,200
but notice we have the flexibility now of really starting

156
00:08:49,200 --> 00:08:52,770
to create something that's quite powerful. So I can store

157
00:08:52,770 --> 00:08:57,950
information in a database, for example, and still query it using PowerShell.

158
00:08:57,950 --> 00:09:01,640
And then, of course, I can wrap that into functions as well.

159
00:09:01,640 --> 00:09:06,140
Okay, so let me just clear the bottom section here and drag down.

160
00:09:06,140 --> 00:09:08,870
Now we can also go a little bit further as well.

161
00:09:08,870 --> 00:09:12,360
We can start to create functions that would allow us to

162
00:09:12,360 --> 00:09:16,190
add things together, to delete things, whatever it would be.

163
00:09:16,190 --> 00:09:20,840
You can see here I've got a function called MathAnswer,

164
00:09:20,840 --> 00:09:24,740
which takes in two properties called $numberOne and $numberTwo,

165
00:09:24,740 --> 00:09:25,950
which will be numbers.

166
00:09:25,950 --> 00:09:29,350
It also takes a $mathOperator, which is going to be a plus,

167
00:09:29,350 --> 00:09:34,080
a minus, a divide, or a multiply, a ValidateSet,

168
00:09:34,080 --> 00:09:36,240
which is a value that we can utilize.

169
00:09:36,240 --> 00:09:41,240
So if someone says Add, then, of course, it will go ahead and do that.

170
00:09:41,240 --> 00:09:43,180
I can then go in and say, Well, hey,

171
00:09:43,180 --> 00:09:46,240
the $mathOperator that you chose was this one,

172
00:09:46,240 --> 00:09:48,090
and I could then use that.

173
00:09:48,090 --> 00:09:51,240
But you'd also see that I'm using the ValidateSet too.

174
00:09:51,240 --> 00:09:55,640
So if the $mathOperator is equal to the word Add,

175
00:09:55,640 --> 00:10:00,200
then it's going to run through here, and it's going to say Invoke‑AddNumbers.

176
00:10:00,200 --> 00:10:02,490
Now where does Invoke‑AddNumbers come from?

177
00:10:02,490 --> 00:10:05,040
Well, there are a series of separate functions that are

178
00:10:05,040 --> 00:10:11,620
listed here. So each of these functions are available and

179
00:10:11,620 --> 00:10:14,360
can be called by the main function.

180
00:10:14,360 --> 00:10:15,760
So what does this look like?

181
00:10:15,760 --> 00:10:20,700
Well, the beauty here is we can select everything that's here.

182
00:10:20,700 --> 00:10:24,680
So let me go to Function Get‑MathAnswer all the way down to here,

183
00:10:24,680 --> 00:10:27,040
and we'll just execute everything.

184
00:10:27,040 --> 00:10:28,980
Now what does the execute mean?

185
00:10:28,980 --> 00:10:33,440
It actually loads it into memory, so we can then use it.

186
00:10:33,440 --> 00:10:37,700
So now that we've executed it and it's in memory, how do we actually run that?

187
00:10:37,700 --> 00:10:40,240
Well, let me just clear the bottom section here.

188
00:10:40,240 --> 00:10:42,240
And if we scroll all the way back up,

189
00:10:42,240 --> 00:10:49,040
we can actually call Get‑MathAnswer as the function, so Get‑MathAnswer.

190
00:10:49,040 --> 00:10:51,120
And then, of course, I get a property called ‑numberOne.

191
00:10:51,120 --> 00:10:56,030
So let's call that 10. And then I should get another one called ‑numberTwo.

192
00:10:56,030 --> 00:10:58,850
So let's call that 5. And then there'll be another one

193
00:10:58,850 --> 00:11:02,790
called ‑mathoperator. And I can tab through.

194
00:11:02,790 --> 00:11:05,560
So if I say Multiply and do Enter,

195
00:11:05,560 --> 00:11:11,040
it will say Math Operator was Multiply, and the answer is 50.

196
00:11:11,040 --> 00:11:15,890
If I change that and say, Add, you can see 15. So notice what we did here.

197
00:11:15,890 --> 00:11:17,740
We're building the script.

198
00:11:17,740 --> 00:11:21,920
The goal is to build as many things into functions as

199
00:11:21,920 --> 00:11:25,570
possible so that then, when you run the script,

200
00:11:25,570 --> 00:11:29,540
it will execute the various commands that you need. Now how you build

201
00:11:29,540 --> 00:11:32,740
them, let me just close this here, is entirely up to you.

202
00:11:32,740 --> 00:11:36,630
Best practice is obviously declaring variables at the top section and then

203
00:11:36,630 --> 00:11:41,430
any functions and then call any of the functions later on.

204
00:11:41,430 --> 00:11:43,740
So what we can do here, for example,

205
00:11:43,740 --> 00:11:50,540
is because we have a list of functions is we can actually say function Begin.

206
00:11:50,540 --> 00:11:52,300
We could call it something like this.

207
00:11:52,300 --> 00:11:55,740
And then what we can do is,

208
00:11:55,740 --> 00:12:00,480
let's say we're going to say Test‑PopulationOfHawaii, and, you know, that's

209
00:12:00,480 --> 00:12:03,190
going to be the function I'm going to call. And then I'm going to say

210
00:12:03,190 --> 00:12:08,380
Get‑MathAnswer, and maybe that's going to be a function.

211
00:12:08,380 --> 00:12:13,340
And then what we do is we then call Begin.

212
00:12:13,340 --> 00:12:16,520
I actually don't want‑‑‑we can use &, but I'm just going to say Begin for now.

213
00:12:16,520 --> 00:12:20,300
So what that means is, when the script runs,

214
00:12:20,300 --> 00:12:23,240
it will automatically call a function,

215
00:12:23,240 --> 00:12:27,410
and then the function will then call everything else that's needed. Now,

216
00:12:27,410 --> 00:12:32,300
what I would suggest with you is that you somehow either have these so the

217
00:12:32,300 --> 00:12:38,130
end user has to provide the variable values or you obviously have it so

218
00:12:38,130 --> 00:12:40,550
they have to put them in manually into the file.

219
00:12:40,550 --> 00:12:43,340
It really depends on what you're trying to achieve. There

220
00:12:43,340 --> 00:12:45,840
are multiple different ways of doing it.

221
00:12:45,840 --> 00:12:53,000
The key, though, is use the building blocks, use the PowerShell functions, use the variables, and tie them all together.

