Now,
one of the main reasons for working with PowerShell is the ability for you
to create functions that contain all of the things that we've talked about
and allow you to build reusable components.
So the first thing to understand is functions. What is a function?
Well functions are literally the building block of PowerShell scripts,
that's how you construct your code into logical reusable components.
They are reusable throughout the entire script,
and they can contain variables, parameters,
statements, and even calls to other functions as needed.
Now what we can do is we can create functions that
use arguments and also parameters.
Now you may wonder what the difference is so let's look at what the
difference is between an argument and a parameter. Now arguments
are not specified within the function,
so there's no property that needs to be filled in.
Arguments are populated simply by passing values at the point of execution.
So if my PowerShell script is called testps1,
I would call it as testps1 space and then whatever I typed as spaced values,
they would be the arguments and the values for those arguments are
retrieved using the offset or the ID identifier. A parameter,
on the other hand,
is defined as a variable in the function. Parameter values
are populated when you call the function.
So we would load the testps1 and then I would call a
function that would say new message,
and then I would have a property that I would populate with a value.
Now, parameters can have properties and parameters can be mandatory or optional.
So how do we create a basic PowerShell function?
Well, here is one that we created,
so function Get‑Answer, it's going to return me an
answer, it's going to ask me a question,
and then I'm going to give the answer. So you can see
we're using a few components here.
The first one is a variable inside the function called question,
and then we're using something called Read‑Host.
So I'm calling a function,
and it's going to ask the user for a piece of information.
It will say what is the capital city in Australia,
which in all fairness as a side note,
most people get wrong, it's not Sydney or anything
else like that, it's something else.
So the answer is clearly written in the function,
but I'm using an if statement to render that.
So if the question, so the returned value from the question equals Canberra,
then that's correct. Write the message in green to the home page
there on the front screen, or if it's incorrect,
write the message again.
So a basic PowerShell function is using the components
that we already know and learned.
So what about if we change that to use arguments because the original
one is requesting something from the user. So what about if we allow
them to just put a value in as part of it?
So what we can do here is we've got this function, Get‑Answer, and
you'll notice something different has happened.
Now it means that somebody can type get space answer space, and then in quotes,
they can type what they think the answer would be,
or maybe they could type their name, or maybe they could do something else.
So in this instance,
I'm asking for a name. So they would type Get‑Answer
Liam, and when the question popped up, it would say Hi,
read the value from the offset ID from what's called args,
$args is a default way of retrieving arguments at the end of a
function or a call, and it would say, Hi,
Liam, what is the capital city in Australia and then
the same piece would then go ahead.
Now what about if we change the PowerShell function
to utilize variables this time?
So once again, same piece of PowerShell, but this time we're not
using args, we've now got Get‑Answer, and then in the parentheses
right as you call the function is $name.
That means this function cannot be called without passing a value.
So to utilize this, I would have to say,
Get‑Answer space Liam and then it would prompt me again, Hi,
Liam, what is the capital city, and then the same process would work.
So three different ways so far of achieving the same thing.
So what about if we wanted to use parameters instead?
So we're creating a function and want parameters to be
available either mandatory or optional.
So in this instance, I've changed the function.
The function is called Test‑WhatIsCapitalCityofAustralia
because I'm also not prompting with a question.
I want to make this really easy,
so I want someone to be able to type in
Test‑WhatIsCapitalCityofAustralia, and then I have a parameter called
city,
which they can type a value, so they could type
Test‑WhatIsCapitalCityofAustralia and then in quotes they
could type whatever the place would be, Melbourne,
Perth, etc, but I don't want them to type that.
I want them to be able to tab through specific values
when they execute this function.
So we do what's called a ValidateSet. You can see
I've got four options available,
and then as they pick the right value from the variable because the
if evaluation now changes to be check the parameter value is equal to
what I'm needing, then it goes ahead.
So what are some of the common function enhancements because you saw we
did the function multiple times, so what are the enhancements? Well the
first one is comments. We put comments in to enhance the functions so
anybody goes in to modify or look at them,
they can see what this function is all about. Then it could
be error handling where we capture in or try catching etc
errors that could take place.
Then, of course, console messages.
You can see that I'm using the write host and then putting a
message out into the console in different colors.
It could be that we use loop statements to loop around sets of
data, or we could be using flow logic to be able to say if,
else, catch, etc. all the way around, even switch statements.
And then, of course,
we can output the results into something that we wish to use.
So let's have a look at a basic script that would iterate functions.
So I created a new function called Get‑Files.
The Get‑Files has a parameter called file type,
which is going to expect me to type the file type I wish to get, so is it a
Docx, is it an Excel, Is it a PDF, and then it calls a standard out‑of‑the‑box
cmdlet called Get‑ChildItem, and then I have a path,
and then it has recurse and force.
So what we can see here is when I simply type Get‑Files space txt, it will
then go and retrieve all the txt files, or the text files, and iterate a
list for me in one go, so really straightforward.
Another option could be well what about if you wanted
to create picker values instead?
So remember we were doing our check in the population of specific cities.
I could also create what's referred to as a new class.
So, for example,
if we're looking at the cities again and saying what's the population of city A,
B, and C, and D, etc, then I can create what's called an autocomplete
value, a custom set of values so that when the end user goes to call that
function and it requires a user interaction,
they don't have to type anything, they can select from here.
Now,
this is what's called an IValidateSetValuesGenerator, so kind of like what
we did with the ValidateSet where we give you picker options, this means we
have a series of values that we can utilize.
So what does this look like in the question function?
Well, if we start our function out,
test the population of Hawaii, the first thing I have is a ValidateSet,
which is Cities, which comes from the previous function that we just looked at,
which is generating values and then it's a variable called Answer.
We then have the if statement that's going to look
for the answer of 1.4 million,
which is 1 of those items. Instead of me having to statically write
the values in the ValidateSet, I'm just calling the object that I
created previously, and then of course, when the end user calls it,
they'll call Test‑PopulationOfHawaii space, then they can
press the Tab key, it will say Answer,
and then they can tab again and it will iterate through the
values that are in that city's object.
So it's all about creating something that's easy for the
end user or even yourself to use again.
Now, if we talk about math functions in a basic one,
you can see that I'm defining some math operators here because I want to
create a function that basically does my math for me, so add,
subtract, multiply,
divide. So you can see I've created a ValidateSet and the
ValidateSet here, I can check the operator, so I can say if
the mathoperator equals Add,
then I've got another method called Invoke‑AddNumbers
and it will add the two numbers.
I've got one that says well elseif is equal to Subtract,
then use the another function called Invoke‑SubtractNumbers.
If it's a multiply,
do the multiply, if it's a divide, do the divide. So you can see how we
start to build these scripts that we take all the things that we've done
so far operators equal to if, else, elseif, else, etc. and add them all together to create these scripts.