1
00:00:00,040 --> 00:00:00,940
Now,

2
00:00:00,940 --> 00:00:05,050
one of the main reasons for working with PowerShell is the ability for you

3
00:00:05,050 --> 00:00:09,090
to create functions that contain all of the things that we've talked about

4
00:00:09,090 --> 00:00:12,840
and allow you to build reusable components.

5
00:00:12,840 --> 00:00:16,020
So the first thing to understand is functions. What is a function?

6
00:00:16,020 --> 00:00:20,400
Well functions are literally the building block of PowerShell scripts,

7
00:00:20,400 --> 00:00:25,710
that's how you construct your code into logical reusable components.

8
00:00:25,710 --> 00:00:29,340
They are reusable throughout the entire script,

9
00:00:29,340 --> 00:00:31,540
and they can contain variables, parameters,

10
00:00:31,540 --> 00:00:36,040
statements, and even calls to other functions as needed.

11
00:00:36,040 --> 00:00:39,260
Now what we can do is we can create functions that

12
00:00:39,260 --> 00:00:43,240
use arguments and also parameters.

13
00:00:43,240 --> 00:00:46,570
Now you may wonder what the difference is so let's look at what the

14
00:00:46,570 --> 00:00:51,540
difference is between an argument and a parameter. Now arguments

15
00:00:51,540 --> 00:00:53,690
are not specified within the function,

16
00:00:53,690 --> 00:00:56,840
so there's no property that needs to be filled in.

17
00:00:56,840 --> 00:01:02,780
Arguments are populated simply by passing values at the point of execution.

18
00:01:02,780 --> 00:01:06,370
So if my PowerShell script is called testps1,

19
00:01:06,370 --> 00:01:13,440
I would call it as testps1 space and then whatever I typed as spaced values,

20
00:01:13,440 --> 00:01:18,290
they would be the arguments and the values for those arguments are

21
00:01:18,290 --> 00:01:24,260
retrieved using the offset or the ID identifier. A parameter,

22
00:01:24,260 --> 00:01:25,360
on the other hand,

23
00:01:25,360 --> 00:01:30,500
is defined as a variable in the function. Parameter values

24
00:01:30,500 --> 00:01:33,440
are populated when you call the function.

25
00:01:33,440 --> 00:01:37,680
So we would load the testps1 and then I would call a

26
00:01:37,680 --> 00:01:40,480
function that would say new message,

27
00:01:40,480 --> 00:01:44,620
and then I would have a property that I would populate with a value.

28
00:01:44,620 --> 00:01:51,440
Now, parameters can have properties and parameters can be mandatory or optional.

29
00:01:51,440 --> 00:01:54,640
So how do we create a basic PowerShell function?

30
00:01:54,640 --> 00:01:56,310
Well, here is one that we created,

31
00:01:56,310 --> 00:01:59,130
so function Get‑Answer, it's going to return me an

32
00:01:59,130 --> 00:02:01,640
answer, it's going to ask me a question,

33
00:02:01,640 --> 00:02:03,880
and then I'm going to give the answer. So you can see

34
00:02:03,880 --> 00:02:05,690
we're using a few components here.

35
00:02:05,690 --> 00:02:09,260
The first one is a variable inside the function called question,

36
00:02:09,260 --> 00:02:11,560
and then we're using something called Read‑Host.

37
00:02:11,560 --> 00:02:13,070
So I'm calling a function,

38
00:02:13,070 --> 00:02:16,280
and it's going to ask the user for a piece of information.

39
00:02:16,280 --> 00:02:20,140
It will say what is the capital city in Australia,

40
00:02:20,140 --> 00:02:21,670
which in all fairness as a side note,

41
00:02:21,670 --> 00:02:24,210
most people get wrong, it's not Sydney or anything

42
00:02:24,210 --> 00:02:26,340
else like that, it's something else.

43
00:02:26,340 --> 00:02:29,760
So the answer is clearly written in the function,

44
00:02:29,760 --> 00:02:32,440
but I'm using an if statement to render that.

45
00:02:32,440 --> 00:02:38,360
So if the question, so the returned value from the question equals Canberra,

46
00:02:38,360 --> 00:02:42,970
then that's correct. Write the message in green to the home page

47
00:02:42,970 --> 00:02:45,940
there on the front screen, or if it's incorrect,

48
00:02:45,940 --> 00:02:47,540
write the message again.

49
00:02:47,540 --> 00:02:50,840
So a basic PowerShell function is using the components

50
00:02:50,840 --> 00:02:54,840
that we already know and learned.

51
00:02:54,840 --> 00:02:58,700
So what about if we change that to use arguments because the original

52
00:02:58,700 --> 00:03:03,290
one is requesting something from the user. So what about if we allow

53
00:03:03,290 --> 00:03:07,640
them to just put a value in as part of it?

54
00:03:07,640 --> 00:03:13,200
So what we can do here is we've got this function, Get‑Answer, and

55
00:03:13,200 --> 00:03:15,600
you'll notice something different has happened.

56
00:03:15,600 --> 00:03:23,200
Now it means that somebody can type get space answer space, and then in quotes,

57
00:03:23,200 --> 00:03:27,390
they can type what they think the answer would be,

58
00:03:27,390 --> 00:03:31,800
or maybe they could type their name, or maybe they could do something else.

59
00:03:31,800 --> 00:03:33,080
So in this instance,

60
00:03:33,080 --> 00:03:37,260
I'm asking for a name. So they would type Get‑Answer

61
00:03:37,260 --> 00:03:41,010
Liam, and when the question popped up, it would say Hi,

62
00:03:41,010 --> 00:03:44,920
read the value from the offset ID from what's called args,

63
00:03:44,920 --> 00:03:50,900
$args is a default way of retrieving arguments at the end of a

64
00:03:50,900 --> 00:03:53,300
function or a call, and it would say, Hi,

65
00:03:53,300 --> 00:03:56,270
Liam, what is the capital city in Australia and then

66
00:03:56,270 --> 00:03:59,640
the same piece would then go ahead.

67
00:03:59,640 --> 00:04:02,380
Now what about if we change the PowerShell function

68
00:04:02,380 --> 00:04:06,040
to utilize variables this time?

69
00:04:06,040 --> 00:04:09,890
So once again, same piece of PowerShell, but this time we're not

70
00:04:09,890 --> 00:04:15,270
using args, we've now got Get‑Answer, and then in the parentheses

71
00:04:15,270 --> 00:04:18,590
right as you call the function is $name.

72
00:04:18,590 --> 00:04:23,170
That means this function cannot be called without passing a value.

73
00:04:23,170 --> 00:04:25,450
So to utilize this, I would have to say,

74
00:04:25,450 --> 00:04:30,830
Get‑Answer space Liam and then it would prompt me again, Hi,

75
00:04:30,830 --> 00:04:35,040
Liam, what is the capital city, and then the same process would work.

76
00:04:35,040 --> 00:04:38,840
So three different ways so far of achieving the same thing.

77
00:04:38,840 --> 00:04:42,340
So what about if we wanted to use parameters instead?

78
00:04:42,340 --> 00:04:45,210
So we're creating a function and want parameters to be

79
00:04:45,210 --> 00:04:49,240
available either mandatory or optional.

80
00:04:49,240 --> 00:04:51,630
So in this instance, I've changed the function.

81
00:04:51,630 --> 00:04:56,440
The function is called Test‑WhatIsCapitalCityofAustralia

82
00:04:56,440 --> 00:04:59,580
because I'm also not prompting with a question.

83
00:04:59,580 --> 00:05:02,410
I want to make this really easy,

84
00:05:02,410 --> 00:05:04,500
so I want someone to be able to type in

85
00:05:04,500 --> 00:05:09,850
Test‑WhatIsCapitalCityofAustralia, and then I have a parameter called

86
00:05:09,850 --> 00:05:10,940
city,

87
00:05:10,940 --> 00:05:13,760
which they can type a value, so they could type

88
00:05:13,760 --> 00:05:17,220
Test‑WhatIsCapitalCityofAustralia and then in quotes they

89
00:05:17,220 --> 00:05:19,960
could type whatever the place would be, Melbourne,

90
00:05:19,960 --> 00:05:22,920
Perth, etc, but I don't want them to type that.

91
00:05:22,920 --> 00:05:26,320
I want them to be able to tab through specific values

92
00:05:26,320 --> 00:05:28,120
when they execute this function.

93
00:05:28,120 --> 00:05:30,760
So we do what's called a ValidateSet. You can see

94
00:05:30,760 --> 00:05:32,330
I've got four options available,

95
00:05:32,330 --> 00:05:36,220
and then as they pick the right value from the variable because the

96
00:05:36,220 --> 00:05:42,750
if evaluation now changes to be check the parameter value is equal to

97
00:05:42,750 --> 00:05:46,840
what I'm needing, then it goes ahead.

98
00:05:46,840 --> 00:05:50,060
So what are some of the common function enhancements because you saw we

99
00:05:50,060 --> 00:05:53,520
did the function multiple times, so what are the enhancements? Well the

100
00:05:53,520 --> 00:05:58,160
first one is comments. We put comments in to enhance the functions so

101
00:05:58,160 --> 00:06:01,000
anybody goes in to modify or look at them,

102
00:06:01,000 --> 00:06:03,990
they can see what this function is all about. Then it could

103
00:06:03,990 --> 00:06:07,790
be error handling where we capture in or try catching etc

104
00:06:07,790 --> 00:06:09,720
errors that could take place.

105
00:06:09,720 --> 00:06:11,460
Then, of course, console messages.

106
00:06:11,460 --> 00:06:15,180
You can see that I'm using the write host and then putting a

107
00:06:15,180 --> 00:06:18,740
message out into the console in different colors.

108
00:06:18,740 --> 00:06:22,710
It could be that we use loop statements to loop around sets of

109
00:06:22,710 --> 00:06:27,190
data, or we could be using flow logic to be able to say if,

110
00:06:27,190 --> 00:06:30,810
else, catch, etc. all the way around, even switch statements.

111
00:06:30,810 --> 00:06:31,650
And then, of course,

112
00:06:31,650 --> 00:06:36,240
we can output the results into something that we wish to use.

113
00:06:36,240 --> 00:06:41,740
So let's have a look at a basic script that would iterate functions.

114
00:06:41,740 --> 00:06:45,440
So I created a new function called Get‑Files.

115
00:06:45,440 --> 00:06:49,220
The Get‑Files has a parameter called file type,

116
00:06:49,220 --> 00:06:53,200
which is going to expect me to type the file type I wish to get, so is it a

117
00:06:53,200 --> 00:06:59,430
Docx, is it an Excel, Is it a PDF, and then it calls a standard out‑of‑the‑box

118
00:06:59,430 --> 00:07:03,740
cmdlet called Get‑ChildItem, and then I have a path,

119
00:07:03,740 --> 00:07:06,340
and then it has recurse and force.

120
00:07:06,340 --> 00:07:13,320
So what we can see here is when I simply type Get‑Files space txt, it will

121
00:07:13,320 --> 00:07:17,810
then go and retrieve all the txt files, or the text files, and iterate a

122
00:07:17,810 --> 00:07:22,140
list for me in one go, so really straightforward.

123
00:07:22,140 --> 00:07:25,400
Another option could be well what about if you wanted

124
00:07:25,400 --> 00:07:27,940
to create picker values instead?

125
00:07:27,940 --> 00:07:34,740
So remember we were doing our check in the population of specific cities.

126
00:07:34,740 --> 00:07:37,930
I could also create what's referred to as a new class.

127
00:07:37,930 --> 00:07:39,050
So, for example,

128
00:07:39,050 --> 00:07:43,820
if we're looking at the cities again and saying what's the population of city A,

129
00:07:43,820 --> 00:07:48,550
B, and C, and D, etc, then I can create what's called an autocomplete

130
00:07:48,550 --> 00:07:53,960
value, a custom set of values so that when the end user goes to call that

131
00:07:53,960 --> 00:07:57,140
function and it requires a user interaction,

132
00:07:57,140 --> 00:08:00,540
they don't have to type anything, they can select from here.

133
00:08:00,540 --> 00:08:00,730
Now,

134
00:08:00,730 --> 00:08:05,400
this is what's called an IValidateSetValuesGenerator, so kind of like what

135
00:08:05,400 --> 00:08:10,210
we did with the ValidateSet where we give you picker options, this means we

136
00:08:10,210 --> 00:08:12,630
have a series of values that we can utilize.

137
00:08:12,630 --> 00:08:16,340
So what does this look like in the question function?

138
00:08:16,340 --> 00:08:18,390
Well, if we start our function out,

139
00:08:18,390 --> 00:08:23,670
test the population of Hawaii, the first thing I have is a ValidateSet,

140
00:08:23,670 --> 00:08:28,290
which is Cities, which comes from the previous function that we just looked at,

141
00:08:28,290 --> 00:08:33,640
which is generating values and then it's a variable called Answer.

142
00:08:33,640 --> 00:08:36,010
We then have the if statement that's going to look

143
00:08:36,010 --> 00:08:38,420
for the answer of 1.4 million,

144
00:08:38,420 --> 00:08:42,320
which is 1 of those items. Instead of me having to statically write

145
00:08:42,320 --> 00:08:46,840
the values in the ValidateSet, I'm just calling the object that I

146
00:08:46,840 --> 00:08:50,500
created previously, and then of course, when the end user calls it,

147
00:08:50,500 --> 00:08:55,340
they'll call Test‑PopulationOfHawaii space, then they can

148
00:08:55,340 --> 00:08:57,630
press the Tab key, it will say Answer,

149
00:08:57,630 --> 00:09:01,760
and then they can tab again and it will iterate through the

150
00:09:01,760 --> 00:09:04,460
values that are in that city's object.

151
00:09:04,460 --> 00:09:07,830
So it's all about creating something that's easy for the

152
00:09:07,830 --> 00:09:10,940
end user or even yourself to use again.

153
00:09:10,940 --> 00:09:13,870
Now, if we talk about math functions in a basic one,

154
00:09:13,870 --> 00:09:17,960
you can see that I'm defining some math operators here because I want to

155
00:09:17,960 --> 00:09:21,890
create a function that basically does my math for me, so add,

156
00:09:21,890 --> 00:09:22,860
subtract, multiply,

157
00:09:22,860 --> 00:09:27,680
divide. So you can see I've created a ValidateSet and the

158
00:09:27,680 --> 00:09:31,300
ValidateSet here, I can check the operator, so I can say if

159
00:09:31,300 --> 00:09:33,410
the mathoperator equals Add,

160
00:09:33,410 --> 00:09:36,530
then I've got another method called Invoke‑AddNumbers

161
00:09:36,530 --> 00:09:38,040
and it will add the two numbers.

162
00:09:38,040 --> 00:09:42,340
I've got one that says well elseif is equal to Subtract,

163
00:09:42,340 --> 00:09:46,040
then use the another function called Invoke‑SubtractNumbers.

164
00:09:46,040 --> 00:09:47,190
If it's a multiply,

165
00:09:47,190 --> 00:09:51,670
do the multiply, if it's a divide, do the divide. So you can see how we

166
00:09:51,670 --> 00:09:54,900
start to build these scripts that we take all the things that we've done

167
00:09:54,900 --> 00:10:03,000
so far operators equal to if, else, elseif, else, etc. and add them all together to create these scripts.

