There's a lot that we can do with arrays and a lot of things for me to show you,
so let's jump back into the console,
and let me demonstrate a number of ways of working with arrays in PowerShell.
Working with arrays in PowerShell, really not that difficult.
Let me walk you through a few things so you can see these things in action.
I'm going to begin by creating a variable,
$a, and give it the values 1 through 10.
I'm using the range operator, the dot dot (..),
and $a, if I I look at it, has the values 1 through 10.
PowerShell will automatically expand the array.
You don't have to do anything special, just access the variable.
Now I can also test if $a is an array.
Now we're going to look at this operator later in the course,
but this is a way to test if an object is of a certain type,
and technically an array is a specific .NET type,
and this returns true.
So it is an array.
And this object has a property that you might want to use called count.
And this will tell you how many items are in the array.
And so in this case, obviously, there are 10.
Now even though I just showed you that $a is an array object,
really, what is in the array, that is what matters.
If I pipe $a to Get‑Member, you can see that the results are Int32 or numbers.
Let's look at this another way.
Let's create another variable, $b.
And I'm going to add some string values to it.
And let's see how many are in there, and I've got 5.
So now what I want to do is I'm going to show you how to
access individual elements of an array, and we do this by counting from 0,
the index count.
And so you can reference an individual element of an array by
putting that index number in square brackets.
So $b[0] is foo.
One, which would be the next item in the list, obviously is bar.
And you can also start at the end, and you can do that by using ‑1,
which will give you the last item in the array.
And you can go backwards.
So I can do ‑2 and so on.
Or you can also use a range operator here and say,
Hey, get me the elements 1 through 3, remember,
always starting counting at 0, and there PowerShell does that.
All right, let's create an empty array.
So I'm going to initialize an array $c,
and we do that with the at symbol and two parentheses‑‑@().
Now I'm going to add some items to the array,
and we do this with the += operator.
So I'm going to add the string localhost.
And then I'm going to add the environmental
variable for the local computer name.
And then I'm going to add another server name that I know is not on my network.
So what's in $c?
And we see three computer names.
Now I show this to you because this is an example of how you
might want to use arrays in PowerShell.
You see a command in PowerShell called Test‑WSMan,
which can be used to test if PowerShell remoting is enabled.
And you say, Hey, it'll take a list of computer names.
Well, I have computer names here, so let me try that.
So I do Test‑WSMan and then do $c, and it fails.
So I look at the Test‑WSMan cmdlet and look at that ‑ComputerName parameter.
PowerShell tells me that this takes a System.String,
and the way that it's written there means it only takes a single string.
It can't handle an array of strings.
Not to worry.
Let's look at another network testing cmdlet.
And let's test‑connection, which has a ‑TargetName parameter.
Now here's the difference, and this is what I wanted to show you.
The ‑TargetName parameter also takes strings,
but notice that double‑square bracket.
That indicates that you can pass multiple computer names
all at the same time to this parameter.
‑ComputerName, up in Test‑WSMan, does not.
It only takes a single string.
-TargetName, in test‑connection,
will take multiple computer names or multiple strings.
And here's a quick look at what the command looks like.
I'm just going to do a single ping to the localhost.
So that's the object that I can expect.
With that in mind then, I can create a PowerShell expression like this.
So I'm going to do Test‑Connection, and I'm going to take all my computer names.
I'm just pinging the IPv4 address just one time.
Now every result that comes through is going to look like
that object that we just looked at.
And I'm going to filter with Where‑Object where the status of
that incoming object is equal to success.
For each result that I get, I'm going to do this.
Now ForEach‑Object has a special way of working.
Normally, we'd just do the ‑Process parameter,
which is a script block that runs once for every object piped in.
But there's also parameter ‑Begin,
which is only run once before any pipeline input is processed.
So I'm going to initialize an array, $c, in that block.
In the ‑Process script block, I'm going to add the Destination property to $c,
which will basically be the computer name.
And then after I've processed everything,
I'm going to just use write‑Host just to give me a little message to say,
Hey, I finished testing.
So we'll run that, pinging all the computers,
filtering,
and then only adding those that were successfully pinged
to $c and then the end block runs, and I get my message that testing is complete,
which means I can now look at $c and see,
Okay,
these are the computers that I was able to ping or that
Test‑Connection was able to work with.
There is a help topic you should look at to get more detail about arrays,
help about_arrays.
So you can see that there.
I'm not going to scroll through that. I'll let you read that on your own.