So let's go back into the PowerShell console,
and we'll create a function within a PowerShell script.
We'll then make another function with parameters within the PowerShell script.
And then we'll look at how we tie it together and create the basic script.
And then, of course, we'll also execute those as well.
Okay, so after all the things that we've gone through,
the most important thing now is to look at how we construct these
into a script of some description and actually start to use
functions the way that they are intended.
Now, of course, one of the ones that we looked at earlier was this approach here,
the ability for us to use a parameter and then obviously pass various
things to it in order for that function to actually execute.
Now what I've got here are a couple of $outputPaths and $paths,
so I'm actually going to right‑click these variables, Run
Selection, and that will just make sure those are there.
And then I'm just going to kind of scroll that down a little bit here
so it's out of the way. Now what I want to do is kind of talk about
some different ways that we can kind of create functions and classes
and things that we can execute.
Now, of course,
the first thing here is to look at how we get interaction with individual users.
So what I'm going to do is leave the PowerShell kind of
Visual Studio Code open for a second,
and I'm going to launch Windows Terminal again, so as administrator,
run that. Wait for that to load and just clear that one.
And then, of course, I'm going to make sure that we're using PowerShell 7.
So let's just clear that message here.
And then we're good to go.
And what I'm going to do is just paste a function directly into this one.
And what you can see is the function is called Get‑Answer. I actually have
some funny characters so I'll probably have to do that again in a second. But,
basically, there's a question called Read‑Host "What is the Capital City of
Australia?" And then we're combining it with an if statement and some colors
and a few other bits and pieces.
So let me just clear that, and I'll just remove the extra characters.
That is an important thing to kind of remember is that often when you're copying
and pasting things around from other kinds of applications,
you may end up with extra characters that don't really make any sense.
Okay, so we have them in.
So what I'll do now is just enter that.
So we're thinking about building a PowerShell script.
We want some user interaction, so I'm going to say Get‑Answer and just Enter.
And, of course, instantly it provides the user the ability to type something.
So I'm going to say Sydney and Enter, and it will say,
Incorrect!! You entered (the value you typed) which is incorrect,
please try again. So I'm going to try again.
And, of course, as everybody knows, the actual capital is Canberra.
And of course, it says, Correct!! You entered (the value). So a simple approach.
But you can have a PowerShell script that literally uses
what's called, if I just select this here,
Read‑Host and will ask the end user for some kind of interaction.
Now that's a great way of doing it.
But what about if we wanted to be able to pass those values
maybe as part of the function instead?
So I'm now just going to copy and paste another one in.
So here it is.
It's the same principle again, so get the answer.
And this time it will say $question = Read‑Host "Hi (something).
What is the Capital City in Australia?" And then,
of course, it has the same thing.
Now notice we're using something different, and we used this previously.
So we're using what's called arguments, though, in this instance,
we're expecting the end user to type something. So I can say
Get‑Answer "Liam Cleary" and Enter. And it will say, Hi Liam
Cleary, what is the Capital City in Australia? And then because
I already know the answer, I can say Canberra, and it gives me the value.
So, once again, a different way,
it's still utilizing the Read‑Host option for the end user to provide
information or the admin to provide information.
However, what it's doing is making it more personal.
Now, of course, we know that we could go a little step further here,
and we could change that from using an argument to actually using a variable.
So you can see here we've got the same kind of function again,
so we're going to say Get‑Answer.
And this time it requires a name.
So I'm going to say in quotes (can't even spell my own
name) "Liam Cleary" Enter. And it will say,
Hi, Liam.
What's the Capital City?
Canberra, and correct.
So a couple different ways.
We used arguments.
We used Read‑Host.
We used a combination of those two.
Plus, we changed it to use a variable.
Now what about if we want to go a little step further?
We want to be able to provide a series of values so that
the end user can just kind of pick.
Now that would make more sense.
Now that helps us by restricting it to specific values.
It stops people spelling things incorrectly.
And it also means that from a code perspective,
we already know what the answers are that we should be expecting.
So I'm now just going to copy and paste another one in.
So here we are.
This is a new function.
It's called Test‑WhatIsCapitalCityofAustralia. So we've removed the question.
We've put that into the function. And then you'll see we've got
this Param section here where we've said go and get the parameter,
so provide one as a mandatory one called $city. And then we have
what's called ValidateSet, which provides four specific options.
So what does that look like for the person executing this?
So I'm going to say Test‑WhatIsCapitalCityofAustralia. Then I
can do a tab here, and you'll see ‑city.
And then when I tab, it lets me loop through the various options.
So if I choose Melbourne, it's going to say Incorrect.
If I go back here, try Canberra, I get the correct value.
So, again, we change the input of the script to then have a predetermined
list of values so that it makes life a little bit easier.
Now, of course, there are lots of other ways of interacting with scripts.
So let's go back to Visual Studio Code,
and what we can see here is I've got some other options available to us. So you
can see I'm creating what's called an Autocomplete option,
which contains all the values.
Now what I wanted to do here is I wanted to use the same
mechanism that we had before where we're saying, What's the
Capital City, and providing some values.
But I wanted those values to come from somewhere else.
So maybe from an array or maybe a JSON file or maybe an XML.
So what we can do, I'm going to slide this over here, is you
can see we can create what's called a class.
So inside the PowerShell script,
we create a class for the definition of the content.
So, for example, this one is an IValidateSetValuesGenerator.
What that means is it's going to create a list of values, and it can be dynamic.
So at the moment I have static values typed into an array.
But that could be a JSON call, an XML call, a web
service call, whatever it would be.
So I have this class.
I've defined it as a string called GetValidValues. And
then I can return a city object.
So I'm going to execute that command. That will then
populate here ready for me to use it.
So this $Cities option should be valid now for me to use.
Now what we can do is like we did with the Australia one, I
want to somehow test the population of Hawaii.
So I've created a function, and you can see how Visual Studio Code is
great at rendering this, color coding the whole thing.
So what I've done here,
the different line is this one where the ValidateSet instead of
it being an array of values that we type,
it's actually calling into that specific class called Cities,
which is listed here.
So if I was to go ahead and just select this function and then go Run Selection,
so now we have it.
Now I can say Test‑PopulationOfHawaii, and then it finds the property ‑Answer,
which is the variable.
And then if I just tab,
we're getting numbers now. If I just go back here, 1.4, 2 million, 500 Thousand,
750 Thousand. So no specific order.
But it means that I can go through and select.
If I choose Enter, it tells me it's incorrect.
If I go back here, say 1.4, it tells me it's correct.
So very similar to what we did before,
but notice we have the flexibility now of really starting
to create something that's quite powerful. So I can store
information in a database, for example, and still query it using PowerShell.
And then, of course, I can wrap that into functions as well.
Okay, so let me just clear the bottom section here and drag down.
Now we can also go a little bit further as well.
We can start to create functions that would allow us to
add things together, to delete things, whatever it would be.
You can see here I've got a function called MathAnswer,
which takes in two properties called $numberOne and $numberTwo,
which will be numbers.
It also takes a $mathOperator, which is going to be a plus,
a minus, a divide, or a multiply, a ValidateSet,
which is a value that we can utilize.
So if someone says Add, then, of course, it will go ahead and do that.
I can then go in and say, Well, hey,
the $mathOperator that you chose was this one,
and I could then use that.
But you'd also see that I'm using the ValidateSet too.
So if the $mathOperator is equal to the word Add,
then it's going to run through here, and it's going to say Invoke‑AddNumbers.
Now where does Invoke‑AddNumbers come from?
Well, there are a series of separate functions that are
listed here. So each of these functions are available and
can be called by the main function.
So what does this look like?
Well, the beauty here is we can select everything that's here.
So let me go to Function Get‑MathAnswer all the way down to here,
and we'll just execute everything.
Now what does the execute mean?
It actually loads it into memory, so we can then use it.
So now that we've executed it and it's in memory, how do we actually run that?
Well, let me just clear the bottom section here.
And if we scroll all the way back up,
we can actually call Get‑MathAnswer as the function, so Get‑MathAnswer.
And then, of course, I get a property called ‑numberOne.
So let's call that 10. And then I should get another one called ‑numberTwo.
So let's call that 5. And then there'll be another one
called ‑mathoperator. And I can tab through.
So if I say Multiply and do Enter,
it will say Math Operator was Multiply, and the answer is 50.
If I change that and say, Add, you can see 15. So notice what we did here.
We're building the script.
The goal is to build as many things into functions as
possible so that then, when you run the script,
it will execute the various commands that you need. Now how you build
them, let me just close this here, is entirely up to you.
Best practice is obviously declaring variables at the top section and then
any functions and then call any of the functions later on.
So what we can do here, for example,
is because we have a list of functions is we can actually say function Begin.
We could call it something like this.
And then what we can do is,
let's say we're going to say Test‑PopulationOfHawaii, and, you know, that's
going to be the function I'm going to call. And then I'm going to say
Get‑MathAnswer, and maybe that's going to be a function.
And then what we do is we then call Begin.
I actually don't want‑‑‑we can use &, but I'm just going to say Begin for now.
So what that means is, when the script runs,
it will automatically call a function,
and then the function will then call everything else that's needed. Now,
what I would suggest with you is that you somehow either have these so the
end user has to provide the variable values or you obviously have it so
they have to put them in manually into the file.
It really depends on what you're trying to achieve. There
are multiple different ways of doing it.
The key, though, is use the building blocks, use the PowerShell functions, use the variables, and tie them all together.