1
00:00:02,940 --> 00:00:06,860
There's a lot that we can do with arrays and a lot of things for me to show you,

2
00:00:06,860 --> 00:00:08,620
so let's jump back into the console,

3
00:00:08,620 --> 00:00:14,460
and let me demonstrate a number of ways of working with arrays in PowerShell.

4
00:00:14,460 --> 00:00:18,920
Working with arrays in PowerShell, really not that difficult.

5
00:00:18,920 --> 00:00:23,340
Let me walk you through a few things so you can see these things in action.

6
00:00:23,340 --> 00:00:25,980
I'm going to begin by creating a variable,

7
00:00:25,980 --> 00:00:29,640
$a, and give it the values 1 through 10.

8
00:00:29,640 --> 00:00:32,380
I'm using the range operator, the dot dot (..),

9
00:00:32,380 --> 00:00:36,580
and $a, if I I look at it, has the values 1 through 10.

10
00:00:36,580 --> 00:00:38,800
PowerShell will automatically expand the array.

11
00:00:38,800 --> 00:00:44,340
You don't have to do anything special, just access the variable.

12
00:00:44,340 --> 00:00:47,610
Now I can also test if $a is an array.

13
00:00:47,610 --> 00:00:50,130
Now we're going to look at this operator later in the course,

14
00:00:50,130 --> 00:00:53,650
but this is a way to test if an object is of a certain type,

15
00:00:53,650 --> 00:00:57,170
and technically an array is a specific .NET type,

16
00:00:57,170 --> 00:00:58,590
and this returns true.

17
00:00:58,590 --> 00:01:00,480
So it is an array.

18
00:01:00,480 --> 00:01:05,160
And this object has a property that you might want to use called count.

19
00:01:05,160 --> 00:01:07,490
And this will tell you how many items are in the array.

20
00:01:07,490 --> 00:01:09,890
And so in this case, obviously, there are 10.

21
00:01:09,890 --> 00:01:13,500
Now even though I just showed you that $a is an array object,

22
00:01:13,500 --> 00:01:16,370
really, what is in the array, that is what matters.

23
00:01:16,370 --> 00:01:25,140
If I pipe $a to Get‑Member, you can see that the results are Int32 or numbers.

24
00:01:25,140 --> 00:01:26,890
Let's look at this another way.

25
00:01:26,890 --> 00:01:31,040
Let's create another variable, $b.

26
00:01:31,040 --> 00:01:34,360
And I'm going to add some string values to it.

27
00:01:34,360 --> 00:01:38,440
And let's see how many are in there, and I've got 5.

28
00:01:38,440 --> 00:01:40,350
So now what I want to do is I'm going to show you how to

29
00:01:40,350 --> 00:01:47,290
access individual elements of an array, and we do this by counting from 0,

30
00:01:47,290 --> 00:01:49,640
the index count.

31
00:01:49,640 --> 00:01:53,250
And so you can reference an individual element of an array by

32
00:01:53,250 --> 00:01:56,620
putting that index number in square brackets.

33
00:01:56,620 --> 00:02:02,040
So $b[0] is foo.

34
00:02:02,040 --> 00:02:06,040
One, which would be the next item in the list, obviously is bar.

35
00:02:06,040 --> 00:02:10,460
And you can also start at the end, and you can do that by using ‑1,

36
00:02:10,460 --> 00:02:12,980
which will give you the last item in the array.

37
00:02:12,980 --> 00:02:14,070
And you can go backwards.

38
00:02:14,070 --> 00:02:17,500
So I can do ‑2 and so on.

39
00:02:17,500 --> 00:02:20,230
Or you can also use a range operator here and say,

40
00:02:20,230 --> 00:02:23,270
Hey, get me the elements 1 through 3, remember,

41
00:02:23,270 --> 00:02:28,540
always starting counting at 0, and there PowerShell does that.

42
00:02:28,540 --> 00:02:31,540
All right, let's create an empty array.

43
00:02:31,540 --> 00:02:34,040
So I'm going to initialize an array $c,

44
00:02:34,040 --> 00:02:37,570
and we do that with the at symbol and two parentheses‑‑@().

45
00:02:37,570 --> 00:02:39,710
Now I'm going to add some items to the array,

46
00:02:39,710 --> 00:02:42,780
and we do this with the += operator.

47
00:02:42,780 --> 00:02:46,440
So I'm going to add the string localhost.

48
00:02:46,440 --> 00:02:49,370
And then I'm going to add the environmental

49
00:02:49,370 --> 00:02:51,530
variable for the local computer name.

50
00:02:51,530 --> 00:02:57,940
And then I'm going to add another server name that I know is not on my network.

51
00:02:57,940 --> 00:02:58,940
So what's in $c?

52
00:02:58,940 --> 00:03:01,620
And we see three computer names.

53
00:03:01,620 --> 00:03:05,210
Now I show this to you because this is an example of how you

54
00:03:05,210 --> 00:03:07,720
might want to use arrays in PowerShell.

55
00:03:07,720 --> 00:03:11,140
You see a command in PowerShell called Test‑WSMan,

56
00:03:11,140 --> 00:03:13,630
which can be used to test if PowerShell remoting is enabled.

57
00:03:13,630 --> 00:03:16,540
And you say, Hey, it'll take a list of computer names.

58
00:03:16,540 --> 00:03:19,300
Well, I have computer names here, so let me try that.

59
00:03:19,300 --> 00:03:25,440
So I do Test‑WSMan and then do $c, and it fails.

60
00:03:25,440 --> 00:03:31,540
So I look at the Test‑WSMan cmdlet and look at that ‑ComputerName parameter.

61
00:03:31,540 --> 00:03:34,480
PowerShell tells me that this takes a System.String,

62
00:03:34,480 --> 00:03:39,860
and the way that it's written there means it only takes a single string.

63
00:03:39,860 --> 00:03:43,050
It can't handle an array of strings.

64
00:03:43,050 --> 00:03:43,860
Not to worry.

65
00:03:43,860 --> 00:03:46,560
Let's look at another network testing cmdlet.

66
00:03:46,560 --> 00:03:52,840
And let's test‑connection, which has a ‑TargetName parameter.

67
00:03:52,840 --> 00:03:55,930
Now here's the difference, and this is what I wanted to show you.

68
00:03:55,930 --> 00:03:58,660
The ‑TargetName parameter also takes strings,

69
00:03:58,660 --> 00:04:01,310
but notice that double‑square bracket.

70
00:04:01,310 --> 00:04:05,340
That indicates that you can pass multiple computer names

71
00:04:05,340 --> 00:04:08,720
all at the same time to this parameter.

72
00:04:08,720 --> 00:04:11,630
‑ComputerName, up in Test‑WSMan, does not.

73
00:04:11,630 --> 00:04:14,140
It only takes a single string.

74
00:04:14,140 --> 00:04:15,920
-TargetName, in test‑connection,

75
00:04:15,920 --> 00:04:20,900
will take multiple computer names or multiple strings.

76
00:04:20,900 --> 00:04:25,520
And here's a quick look at what the command looks like.

77
00:04:25,520 --> 00:04:28,300
I'm just going to do a single ping to the localhost.

78
00:04:28,300 --> 00:04:31,210
So that's the object that I can expect.

79
00:04:31,210 --> 00:04:37,940
With that in mind then, I can create a PowerShell expression like this.

80
00:04:37,940 --> 00:04:42,610
So I'm going to do Test‑Connection, and I'm going to take all my computer names.

81
00:04:42,610 --> 00:04:46,540
I'm just pinging the IPv4 address just one time.

82
00:04:46,540 --> 00:04:48,580
Now every result that comes through is going to look like

83
00:04:48,580 --> 00:04:51,640
that object that we just looked at.

84
00:04:51,640 --> 00:04:55,060
And I'm going to filter with Where‑Object where the status of

85
00:04:55,060 --> 00:04:57,460
that incoming object is equal to success.

86
00:04:57,460 --> 00:05:03,050
For each result that I get, I'm going to do this.

87
00:05:03,050 --> 00:05:06,740
Now ForEach‑Object has a special way of working.

88
00:05:06,740 --> 00:05:09,290
Normally, we'd just do the ‑Process parameter,

89
00:05:09,290 --> 00:05:13,240
which is a script block that runs once for every object piped in.

90
00:05:13,240 --> 00:05:15,060
But there's also parameter ‑Begin,

91
00:05:15,060 --> 00:05:19,610
which is only run once before any pipeline input is processed.

92
00:05:19,610 --> 00:05:23,950
So I'm going to initialize an array, $c, in that block.

93
00:05:23,950 --> 00:05:29,830
In the ‑Process script block, I'm going to add the Destination property to $c,

94
00:05:29,830 --> 00:05:33,040
which will basically be the computer name.

95
00:05:33,040 --> 00:05:36,940
And then after I've processed everything,

96
00:05:36,940 --> 00:05:40,440
I'm going to just use write‑Host just to give me a little message to say,

97
00:05:40,440 --> 00:05:41,950
Hey, I finished testing.

98
00:05:41,950 --> 00:05:47,040
So we'll run that, pinging all the computers,

99
00:05:47,040 --> 00:05:47,560
filtering,

100
00:05:47,560 --> 00:05:51,660
and then only adding those that were successfully pinged

101
00:05:51,660 --> 00:05:56,700
to $c and then the end block runs, and I get my message that testing is complete,

102
00:05:56,700 --> 00:06:00,200
which means I can now look at $c and see,

103
00:06:00,200 --> 00:06:00,560
Okay,

104
00:06:00,560 --> 00:06:03,840
these are the computers that I was able to ping or that

105
00:06:03,840 --> 00:06:07,440
Test‑Connection was able to work with.

106
00:06:07,440 --> 00:06:11,700
There is a help topic you should look at to get more detail about arrays,

107
00:06:11,700 --> 00:06:13,120
help about_arrays.

108
00:06:13,120 --> 00:06:14,120
So you can see that there.

109
00:06:14,120 --> 00:06:19,000
I'm not going to scroll through that. I'll let you read that on your own.

