1
00:00:00,040 --> 00:00:02,230
So let's go back into the PowerShell console.

2
00:00:02,230 --> 00:00:05,060
We'll create a custom data object.

3
00:00:05,060 --> 00:00:08,400
We'll first also retrieve data from the custom object by

4
00:00:08,400 --> 00:00:10,770
loading and reusing that custom object.

5
00:00:10,770 --> 00:00:14,260
We'll then test those objects using that ‑IS operator.

6
00:00:14,260 --> 00:00:19,380
Now, PowerShell provides us the ability for using custom data,

7
00:00:19,380 --> 00:00:23,310
different types of data that we can store or retrieve.

8
00:00:23,310 --> 00:00:28,340
Now, the easiest example here would be to create a new variable,

9
00:00:28,340 --> 00:00:35,300
and we can use the @ and the brackets here to actually create an array,

10
00:00:35,300 --> 00:00:37,020
and that's effectively an empty array.

11
00:00:37,020 --> 00:00:40,390
So if I say variable, and just do this,

12
00:00:40,390 --> 00:00:43,630
there'll be no values that are associated to that.

13
00:00:43,630 --> 00:00:48,170
Now, if I wanted to change that and actually utilize values,

14
00:00:48,170 --> 00:00:51,850
then we can simply use text values here,

15
00:00:51,850 --> 00:01:05,740
so Jan, Feb, Mar, Apr, May, and Jun.

16
00:01:05,740 --> 00:01:11,270
Okay, now we can say $variable, and we get our values back,

17
00:01:11,270 --> 00:01:14,140
so it's an array of those objects.

18
00:01:14,140 --> 00:01:18,270
Now, that's a standard array with text values.

19
00:01:18,270 --> 00:01:21,510
Now, we've also created arrays already,

20
00:01:21,510 --> 00:01:27,310
where we've actually created an array by not using the definition of an array,

21
00:01:27,310 --> 00:01:31,750
but by adding the values in like this, which works perfectly fine,

22
00:01:31,750 --> 00:01:33,460
so that's one way of doing it too.

23
00:01:33,460 --> 00:01:37,540
You don't have to declare by using @ and brackets every

24
00:01:37,540 --> 00:01:39,940
time you want to create an array.

25
00:01:39,940 --> 00:01:43,080
However, what that does give us is,

26
00:01:43,080 --> 00:01:46,320
if we just go back here and create the empty array,

27
00:01:46,320 --> 00:01:50,950
is that when we say variable.Add,

28
00:01:50,950 --> 00:01:55,940
we get this kind of function method that's available to us to add entries to it,

29
00:01:55,940 --> 00:01:59,240
whereas if we've created the variable without that,

30
00:01:59,240 --> 00:02:00,470
we don't have that flexibility,

31
00:02:00,470 --> 00:02:04,600
so there is a reason for creating them that way as well.

32
00:02:04,600 --> 00:02:07,250
So let's just go back here and populate that with some values,

33
00:02:07,250 --> 00:02:09,440
okay, so now we have that.

34
00:02:09,440 --> 00:02:12,090
Now, if we want to access the data in here,

35
00:02:12,090 --> 00:02:16,230
you can see that we could just type the variable and it would output the values.

36
00:02:16,230 --> 00:02:19,420
But, of course, as with all sets of data,

37
00:02:19,420 --> 00:02:22,720
we do have the ability to utilize an offset.

38
00:02:22,720 --> 00:02:24,760
So, for example, if I use 0,

39
00:02:24,760 --> 00:02:27,210
that's the first piece that was chopped out in the array,

40
00:02:27,210 --> 00:02:29,650
I get February, sorry, January.

41
00:02:29,650 --> 00:02:36,140
If I do 2, then I'll get March, because, remember, it starts at 0, not at 1.

42
00:02:36,140 --> 00:02:39,610
It also means that I can do this, too.

43
00:02:39,610 --> 00:02:45,190
I can then use different values to be able to retrieve those items from there.

44
00:02:45,190 --> 00:02:50,520
So 0, which is January, 1 is February, 4 would be May,

45
00:02:50,520 --> 00:02:52,890
for example, so I get those values back.

46
00:02:52,890 --> 00:02:57,100
We also have the ability to use, so 0..3,

47
00:02:57,100 --> 00:03:02,640
so using that range operator that we used previously for adding data,

48
00:03:02,640 --> 00:03:06,600
we can use that for retrieving values as well,

49
00:03:06,600 --> 00:03:11,540
so really simple kind of ways of accessing the data.

50
00:03:11,540 --> 00:03:15,980
Now, what's nice is once the data is in a variable,

51
00:03:15,980 --> 00:03:20,230
then of course we can retrieve the data either that way or

52
00:03:20,230 --> 00:03:22,650
we can retrieve it by piping the values.

53
00:03:22,650 --> 00:03:26,870
Now a pipe option allows us to take the values or something

54
00:03:26,870 --> 00:03:30,140
from a command and pass it into the next one.

55
00:03:30,140 --> 00:03:32,460
So I'm going to use something that we've already talked about,

56
00:03:32,460 --> 00:03:34,010
which is called ForEach‑Object.

57
00:03:34,010 --> 00:03:37,740
So I'm going to say take everything that's in the variable,

58
00:03:37,740 --> 00:03:42,900
and for every single one of those, in my script block I want it to do something.

59
00:03:42,900 --> 00:03:47,040
So I'm going to go in and say Write‑Host.

60
00:03:47,040 --> 00:03:54,330
So I'm going to write a value and say The month is,

61
00:03:54,330 --> 00:03:58,640
and then I can say $, I missed my letters there,

62
00:03:58,640 --> 00:04:02,120
PSItem, and close that off.

63
00:04:02,120 --> 00:04:06,740
Now, PSItem is a value that's returned from the ForEach‑Object.

64
00:04:06,740 --> 00:04:09,780
And, of course, when I enter that, that's what it does,

65
00:04:09,780 --> 00:04:12,440
the PSItem is an arbitrary one that's there,

66
00:04:12,440 --> 00:04:15,840
that when you use ForEach‑Object, that's what comes back.

67
00:04:15,840 --> 00:04:19,310
So now I can say, hey, here's my variable of data,

68
00:04:19,310 --> 00:04:22,400
for each item in there, iterate it, and then do something,

69
00:04:22,400 --> 00:04:25,800
and in this instance I'm writing out values to the screen.

70
00:04:25,800 --> 00:04:27,910
So that's one way of doing that.

71
00:04:27,910 --> 00:04:30,850
The second way of doing that is to use the trusty old

72
00:04:30,850 --> 00:04:38,340
foreach statement that we used before, where I can say $item in $variable,

73
00:04:38,340 --> 00:04:41,310
and then do the same thing as we did before,

74
00:04:41,310 --> 00:04:44,970
where we kind of output in that inside the script block here.

75
00:04:44,970 --> 00:04:52,070
So Write‑Host, and I'm just going to say $item, like so.

76
00:04:52,070 --> 00:04:55,760
And, of course, you get January all the way to June, so nice and easy.

77
00:04:55,760 --> 00:04:59,940
So we can use all of the different looping mechanisms that we

78
00:04:59,940 --> 00:05:03,070
used previously to actually get the data back,

79
00:05:03,070 --> 00:05:09,180
which is really the most common thing that you would do with data that you get.

80
00:05:09,180 --> 00:05:13,540
You want to iterate through it, and you want to do something with it.

81
00:05:13,540 --> 00:05:17,500
Now, an array is great, but really it's just a single value,

82
00:05:17,500 --> 00:05:20,210
well, I say single values, but it's multiple values,

83
00:05:20,210 --> 00:05:22,620
but there's just a single value for each one.

84
00:05:22,620 --> 00:05:27,400
So we can also create data, so I'm going to call it variableh,

85
00:05:27,400 --> 00:05:32,740
and then I'm going to say @ and I'm going to use squirrely brackets this time.

86
00:05:32,740 --> 00:05:36,470
Now notice that's not an array this time, that's a hash table.

87
00:05:36,470 --> 00:05:42,670
Now a hash table is a series of keys and values, so let me give you an idea.

88
00:05:42,670 --> 00:05:48,820
So I've got my variableh even, and from here I can say @,

89
00:05:48,820 --> 00:05:51,100
squirrely brackets instead of the regular,

90
00:05:51,100 --> 00:05:59,540
and then I'm going to say Month=5, Name=May,

91
00:05:59,540 --> 00:06:01,780
Season=Spring.

92
00:06:01,780 --> 00:06:07,020
So let's put some of these in quotes,

93
00:06:07,020 --> 00:06:09,520
because they're string values that we need to return back,

94
00:06:09,520 --> 00:06:12,940
so let's do this, and let's do that.

95
00:06:12,940 --> 00:06:15,490
Okay, now notice what happened here.

96
00:06:15,490 --> 00:06:20,820
It says that I have some syntax errors, and that's great,

97
00:06:20,820 --> 00:06:24,330
because what that means is that notice how we would

98
00:06:24,330 --> 00:06:27,910
normally comma separate things, we actually,

99
00:06:27,910 --> 00:06:32,940
when we're doing key‑value pairs, we have to use the semicolon instead.

100
00:06:32,940 --> 00:06:38,460
Now, what that does for us, say if I do variableh and Enter it,

101
00:06:38,460 --> 00:06:41,770
you can see I then get a name and a value,

102
00:06:41,770 --> 00:06:47,040
so Month is 5, Name is May, Season is Spring.

103
00:06:47,040 --> 00:06:49,010
Now, what does that do for us?

104
00:06:49,010 --> 00:06:50,840
Well, let's look at this.

105
00:06:50,840 --> 00:06:54,790
So what we can do is I can say variableh.Count,

106
00:06:54,790 --> 00:06:58,580
and you see I've got all these different things that are there.

107
00:06:58,580 --> 00:07:03,090
I could say Values, and this will go and get me just the values.

108
00:07:03,090 --> 00:07:07,420
Or, I could say get me the Keys, and it gets me the keys.

109
00:07:07,420 --> 00:07:11,470
So you have the ability to store values with a key

110
00:07:11,470 --> 00:07:14,140
that you can then reference later on.

111
00:07:14,140 --> 00:07:18,240
I can also change the order around as well, if I needed to.

112
00:07:18,240 --> 00:07:21,200
Let's think a little bit kind of wider here.

113
00:07:21,200 --> 00:07:23,310
I'm just going to copy something and paste it in,

114
00:07:23,310 --> 00:07:28,230
just because it's easier for the text that I'm using.

115
00:07:28,230 --> 00:07:33,080
So you can see I'm creating a new variable as a hash table and I

116
00:07:33,080 --> 00:07:35,610
have four countries in here with their population,

117
00:07:35,610 --> 00:07:37,540
so I'm going to press Enter here.

118
00:07:37,540 --> 00:07:40,760
Now, what that means is I can go to variable,

119
00:07:40,760 --> 00:07:43,850
and I now get this matrix of a table that basically

120
00:07:43,850 --> 00:07:48,440
says this country has this value, etc.

121
00:07:48,440 --> 00:07:51,740
So how can we retrieve some of this item?

122
00:07:51,740 --> 00:07:54,520
Well, the first option that we've got is like we just did,

123
00:07:54,520 --> 00:08:00,050
we can say variable.Keys, and I get the values of that,

124
00:08:00,050 --> 00:08:03,390
but I don't get the value associated to the keys,

125
00:08:03,390 --> 00:08:07,150
so we need some way of kind of iterating through them.

126
00:08:07,150 --> 00:08:11,460
Now, the simplest way of doing this is to do a foreach statement,

127
00:08:11,460 --> 00:08:15,120
and I can say $key, so that's an empty value,

128
00:08:15,120 --> 00:08:20,540
in, and then I'm going to say variable.Keys.

129
00:08:20,540 --> 00:08:25,360
So what I'm doing is I'm going to iterate through the sets of keys,

130
00:08:25,360 --> 00:08:27,640
and I need to be able to retrieve value.

131
00:08:27,640 --> 00:08:31,140
So I'm now going to create a new variable called output,

132
00:08:31,140 --> 00:08:36,660
and I'm going to use some kind of string manipulation here,

133
00:08:36,660 --> 00:08:42,160
has a population of, and another one of these,

134
00:08:42,160 --> 00:08:44,340
so 1.

135
00:08:44,340 --> 00:08:46,150
So if you've never seen this before,

136
00:08:46,150 --> 00:08:49,200
this is a standard constructing code where you're formatting

137
00:08:49,200 --> 00:08:52,920
a string value and you use the little squirrely brackets

138
00:08:52,920 --> 00:08:55,730
with a number that represents 0, 1, 2, 3,

139
00:08:55,730 --> 00:08:56,860
4, etc.

140
00:08:56,860 --> 00:09:00,630
I'm then going to go in and say for the first value I

141
00:09:00,630 --> 00:09:03,800
want that to be what's called $_, which is the key,

142
00:09:03,800 --> 00:09:09,050
and then the next one is $variable, brackets,

143
00:09:09,050 --> 00:09:11,020
remember, is used for the offset,

144
00:09:11,020 --> 00:09:15,240
but I'm just going to use the identifier piece there and press Enter.

145
00:09:15,240 --> 00:09:22,800
Once I've done that one, then I'm going to say Write‑Output $,

146
00:09:22,800 --> 00:09:29,040
oh, I don't want all of that, I just want the smaller variable.

147
00:09:29,040 --> 00:09:30,370
Let me just change this.

148
00:09:30,370 --> 00:09:32,020
Let me just do Write‑Host instead.

149
00:09:32,020 --> 00:09:35,320
I was trying to write out too much.

150
00:09:35,320 --> 00:09:45,240
(Writing) Output, there we go, and close that out, and Enter.

151
00:09:45,240 --> 00:09:46,850
Now, what happened here?

152
00:09:46,850 --> 00:09:49,430
You can see that I've got loads and loads and loads

153
00:09:49,430 --> 00:09:50,970
of errors all over the place.

154
00:09:50,970 --> 00:09:53,830
It doesn't like what's taken place here.

155
00:09:53,830 --> 00:09:56,940
So let's kind of break this down what it's trying to do.

156
00:09:56,940 --> 00:10:01,740
So what you can see here is we've got a foreach statement,

157
00:10:01,740 --> 00:10:05,770
which is going to go and loop through the values,

158
00:10:05,770 --> 00:10:09,190
and you can see that we're trying to use this $_.

159
00:10:09,190 --> 00:10:11,440
Now hold on a minute, what is a $_?

160
00:10:11,440 --> 00:10:15,410
Well, a $_ is an object that returns back,

161
00:10:15,410 --> 00:10:17,700
yes, wait a minute, I can hear you thinking,

162
00:10:17,700 --> 00:10:24,140
it's like the PSItem, which means it can't be returned in a foreach statement.

163
00:10:24,140 --> 00:10:30,040
So in a foreach statement, we have to use the named objects that come back.

164
00:10:30,040 --> 00:10:33,390
So let's go back through the code, so here's my output,

165
00:10:33,390 --> 00:10:35,390
and we're going to change these values,

166
00:10:35,390 --> 00:10:39,800
because the $_ is only available for a foreach.

167
00:10:39,800 --> 00:10:42,330
So what this is going to be is the key.

168
00:10:42,330 --> 00:10:44,340
Where does the key come from?

169
00:10:44,340 --> 00:10:47,590
From the entry in the beginning of the for loop.

170
00:10:47,590 --> 00:10:52,440
If I then go here and say $key, like so,

171
00:10:52,440 --> 00:10:59,540
I can then say Write‑Host, and I'll just say,

172
00:10:59,540 --> 00:11:03,250
sometimes it's not best to use words that match everything else,

173
00:11:03,250 --> 00:11:05,480
and close that out, and then Enter.

174
00:11:05,480 --> 00:11:06,950
And sure enough, it now works.

175
00:11:06,950 --> 00:11:09,660
It now says Spain has a population of something,

176
00:11:09,660 --> 00:11:13,050
blah, blah, blah, blah, all the way down to India has a population of,

177
00:11:13,050 --> 00:11:13,810
and something.

178
00:11:13,810 --> 00:11:17,760
So It's really important to understand that when you see examples

179
00:11:17,760 --> 00:11:20,580
of PowerShell code that's available to you,

180
00:11:20,580 --> 00:11:23,010
just make sure that you're not mixing and matching.

181
00:11:23,010 --> 00:11:27,370
So aForEach‑Object loop would look very similar,

182
00:11:27,370 --> 00:11:31,160
so let's just kind of show you what that would look like.

183
00:11:31,160 --> 00:11:33,230
I'm not going to run all of it, but show you.

184
00:11:33,230 --> 00:11:39,410
You would do the variable, you would do a ForEach‑Object statement,

185
00:11:39,410 --> 00:11:46,400
like so, and then for this row you would do that same thing that we did,

186
00:11:46,400 --> 00:11:53,340
so 0, some text, 1, close that out.

187
00:11:53,340 --> 00:12:01,360
Then you get access to the _$, and then you can say variable,

188
00:12:01,360 --> 00:12:04,210
in the brackets $_.

189
00:12:04,210 --> 00:12:15,440
Once we have that, Write‑Host $output, and do that, and Enter.

190
00:12:15,440 --> 00:12:18,440
And then, of course, depending if you've got all the variables right,

191
00:12:18,440 --> 00:12:21,930
it'll then write out the variable values that you're expecting.

192
00:12:21,930 --> 00:12:24,990
You noticed why it's come back with a hash table?

193
00:12:24,990 --> 00:12:27,140
Extra points if you can get this right,

194
00:12:27,140 --> 00:12:28,830
but I'm going to give you the answer anyway,

195
00:12:28,830 --> 00:12:34,140
is because the variable that we actually want to iterate is variable.Keys,

196
00:12:34,140 --> 00:12:36,930
not the actual variable itself.

197
00:12:36,930 --> 00:12:40,100
So if we go back here and say Write‑Host and do that,

198
00:12:40,100 --> 00:12:41,740
you'll get all the values now.

199
00:12:41,740 --> 00:12:43,990
So it's really important to understand the definition of

200
00:12:43,990 --> 00:12:46,240
the object that you're trying to get to.

201
00:12:46,240 --> 00:12:48,740
Okay, so let's just clear that for a second.

202
00:12:48,740 --> 00:12:49,450
Now,

203
00:12:49,450 --> 00:12:52,990
one of the last things that we can look at here is really around

204
00:12:52,990 --> 00:12:56,070
creating what's referred to as a custom object.

205
00:12:56,070 --> 00:12:59,500
Now, PowerShell obviously has the PSCustomObject,

206
00:12:59,500 --> 00:13:04,140
and most things get returned as a PSObject.

207
00:13:04,140 --> 00:13:09,640
So I'm just going to copy some code here, and I'm just going to paste over here.

208
00:13:09,640 --> 00:13:12,950
Now you can see the code looks almost identical to the last one,

209
00:13:12,950 --> 00:13:16,420
except the difference is right here we've declared

210
00:13:16,420 --> 00:13:20,060
that object as a PSCustomObject, so we'll just enter that,

211
00:13:20,060 --> 00:13:22,440
so now we have a value.

212
00:13:22,440 --> 00:13:27,580
So if I go back and say variable, notice it lays it out a little bit differently,

213
00:13:27,580 --> 00:13:29,230
but you still get the same values.

214
00:13:29,230 --> 00:13:32,940
You get China, India, America, Spain, and the different ones.

215
00:13:32,940 --> 00:13:33,320
Now,

216
00:13:33,320 --> 00:13:37,860
the nice thing here about using a CustomObject is that we

217
00:13:37,860 --> 00:13:40,700
are able to add things to the object.

218
00:13:40,700 --> 00:13:44,310
Now, I'm just going to copy a value here.

219
00:13:44,310 --> 00:13:46,530
So you can see the value.

220
00:13:46,530 --> 00:13:51,500
I've chosen that same variable, and this time I've piped it into Add‑Member,

221
00:13:51,500 --> 00:13:53,090
because I wish to add a member.

222
00:13:53,090 --> 00:13:59,340
And PSCustomObject has a NoteProperty, so I'm going to add this one to it.

223
00:13:59,340 --> 00:14:03,440
So if I go back to variable now, you'll see that Russia has now been added.

224
00:14:03,440 --> 00:14:08,040
If I go and just copy and paste another one here,

225
00:14:08,040 --> 00:14:10,250
there we go, we can see Norway.

226
00:14:10,250 --> 00:14:16,060
I can enter that, let me just clear the screen, and then do variable.

227
00:14:16,060 --> 00:14:19,030
You'll see we've now added to the original variable

228
00:14:19,030 --> 00:14:22,240
itself because it's a CustomObject.

229
00:14:22,240 --> 00:14:23,780
So that's the beauty of this.

230
00:14:23,780 --> 00:14:26,620
Now, what does that give us over everything else?

231
00:14:26,620 --> 00:14:31,540
Well, let's have a look at how we can retrieve values from this.

232
00:14:31,540 --> 00:14:36,440
Now notice I can just do variable.China and it gives me the value.

233
00:14:36,440 --> 00:14:41,440
I can do variable.Norway and I get those values back too.

234
00:14:41,440 --> 00:14:47,610
So it means I'm able to return the value from the CustomObject really simply.

235
00:14:47,610 --> 00:14:51,480
So, it's a nice way to store values, I suppose,

236
00:14:51,480 --> 00:14:55,740
almost in like an indexable way, where you're able to return the values.

237
00:14:55,740 --> 00:15:00,510
If I wanted to just get everything, then of course I could just do that,

238
00:15:00,510 --> 00:15:04,140
but of course if I tab through, I get the values,

239
00:15:04,140 --> 00:15:06,960
Spain, I get equals, I get all of the regular things,

240
00:15:06,960 --> 00:15:09,980
but all of those become properties on the object,

241
00:15:09,980 --> 00:15:17,000
so it's a nice way of being able to manage information that we need to populate and add in and reuse later.

