Now that we've looked at the different approaches,
what we can do is start to create functions in PowerShell
that wrap some of this capability up.
So to do this, I'm going to right‑click here and launch Visual Studio Code.
That's my preferred tool to be able to do this type of work.
And I've already got a folder that I've opened into.
So I'm going to simply click here and we'll create new,
and we'll call it Ping‑Functions.ps1.
Now what's nice here is, when you're in Visual Studio,
it's going to say do you want to install the recommended extension?
I'm going to click Install.
This will just give you some information about what it's doing.
What this means is I can run the commands directly
inline inside my Visual Studio Code.
You can see it's loading here.
And then of course, you'll see in the top window if I just scroll back down here,
it's going to say, well, which version of PowerShell do you want to use?
So I can say choose a PowerShell version, and I could say Install PowerShell 7.
And of course, it's going to try and launch me out to the web browser,
which it does, and it will say this is how you install it.
Actually, I already have it installed, but just so you can kind of see.
I'm going to close the Get Started and that one out.
Now what we have here is the top section.
I'm going to change over here so I've got my function,
I've got the code area, and I've got my execution section here.
So I'm going to say clear.
And then from here, we can start to write a function.
So let's say function and using the standard name convention for
PowerShell cmdlets where you have some kind of action,
some doing thing, and then the name.
So I'm going to say invoke ping.
I'm then going to say I've got some squirrely brackets there.
So that's going to be our construct of the function.
What I want to do also is put in some type of parameter that says
you need to populate it with this type of value.
So what we can do here is we can do what's called in here so we can say cmd,
and you can see as we type things here, CmdletBinding.
So I can say CmdletBinding, and it populates that value.
I can then do this.
Then underneath it, I can say param and then start to build out my param section,
which will be my parameter or my variable that I want to pass into it.
So I'm going to say parameter, and you'll see it starts to give me IntelliSense.
So I'm going to do this Parameter.
I'm going to say Position,
which is going to be the position that it calls the value.
This will be the very first one.
I'm then going to go in and say do I want to make it mandatory?
Of course I do.
I'm going to say true.
And then, once I've determined what that needs to be,
so I'm going to close out my Parameter option that's there, then,
let me just go here, I then determine what type of parameter it's going to be.
I want it to be a String value.
And then from here, I'm going to say ip.
So that's going to be my variable.
It's going to be a String item, which will effectively be an IP address.
So now that we have that there,
what I want to be able to do is utilize that ping capability.
We'll use the .NET namespace for this one,
$ping = New‑Object,
and then we'll say System., and you can see we get great IntelliSense here.
I'm going to say NetworkInformation.Ping, so I'm creating my new ping object.
And then I can say $ping.Send and then take that ip option and put that there.
So that creates me a function.
Let me just scroll this down a little bit here so we can see the whole thing.
So there's my function.
So I'm going to say select.
I'm going to right‑click and say Run Selection. So that means my
function should now be available. If I do clear and then say
Invoke‑Ping, I should be able to do that IP address and say
192.168.127.141 and press Enter. And what this will do is this will now
go and invoke that ping. It will say go and get my object and then come
back. And of course, it says timed out.
If I change that to 130, for example, I get success.
So we know it's working. It just depends on the IP address. So that's great.
It just returns all of those values. So we can create these reusable
functions to kind of make our code a little bit better instead of
just calling the standard ping function.
Now what's also nice here is we can start to return custom messages as
well. So what we can do here, I'm going to call this results. If I just
execute results, for example, here, just do run, you'll notice here that
it comes back and says you can't call a method that has a null‑valued
expression. Now why is that?
And that's because it's in a function,
and I don't necessarily have an IP address. And we'll just do this,
Run Selection. Okay, now, let's see what results is populated with,
Run Selection, all of those values.
So that means I should be able to say results, and you can see I
can now tab through the various properties. So I can get the
individual items, so it says status.
So what we can do here is it says success.
So if we wanted to change our function a little bit, we can say if
$results.Status equals and, of course, the result value is success
like so. Then I can do an else action as well. And then we'll do
something as simple as‑‑‑ Okay, so let's just select our function
again just to update it. We'll say Run Selection, clear. We can loop
through our commands, invoke ping, and it will come back and say success.
If I change that to 41, it should fail,
come back with something. And depending on the result, it should say failure.
So we can create functions to use multiple times again and again
and again, especially as we're trying to build, for example, the
asset list a little bit later.