1
00:00:01,080 --> 00:00:04,440
Next, I want to move to a set of commands,

2
00:00:04,440 --> 00:00:08,690
and you can install this module from the PowerShell gallery.

3
00:00:08,690 --> 00:00:11,200
I have notes in the demo file on how to do that if

4
00:00:11,200 --> 00:00:12,700
you want to follow along here.

5
00:00:12,700 --> 00:00:16,680
So let's say that you have a completely new command

6
00:00:16,680 --> 00:00:18,670
that you've been assigned, told, hey,

7
00:00:18,670 --> 00:00:22,270
we need you to work with these sets of PowerShell tools, but

8
00:00:22,270 --> 00:00:25,670
you have no idea how to use the tool, so you have to use the

9
00:00:25,670 --> 00:00:32,120
help. My tools that I wrote here, that you can work with, are vegetable commands.

10
00:00:32,120 --> 00:00:34,950
So I have a command called Get‑Vegetable, and we're just

11
00:00:34,950 --> 00:00:39,580
going to look at just the parameters for Get‑Vegetable. And

12
00:00:39,580 --> 00:00:42,510
you can see that it has name,

13
00:00:42,510 --> 00:00:46,740
so it will take a single string, notice there's no square brackets,

14
00:00:46,740 --> 00:00:50,730
the name of vegetable, it's not required, it's positional,

15
00:00:50,730 --> 00:00:55,530
which means I don't have to type ‑name, it takes pipeline input, and it

16
00:00:55,530 --> 00:01:01,020
will accept wildcards, RootOnly is a switch, alright,

17
00:01:01,020 --> 00:01:05,510
so those are really the only parameters for that command.

18
00:01:05,510 --> 00:01:07,920
You know what, let's just run it. It's a get command, you

19
00:01:07,920 --> 00:01:10,800
know, what's the worst that can happen? So let's run

20
00:01:10,800 --> 00:01:14,060
Get‑Vegetable, and there we go.

21
00:01:14,060 --> 00:01:17,960
So I get an object, and the nice thing about this command, these vegetable

22
00:01:17,960 --> 00:01:20,640
commands, is they have nothing to do with technology.

23
00:01:20,640 --> 00:01:25,120
You can just focus on the PowerShell concepts without worrying

24
00:01:25,120 --> 00:01:28,020
about the technology underneath the hood, because you don't need

25
00:01:28,020 --> 00:01:31,140
anything other than the commands and they'll create these kind of

26
00:01:31,140 --> 00:01:33,840
fake vegetable objects for you.

27
00:01:33,840 --> 00:01:34,730
Get‑Vegetable works.

28
00:01:34,730 --> 00:01:36,570
Let's go back and try Get‑Vegetable, let's try that

29
00:01:36,570 --> 00:01:38,390
other parameter, that switch parameter.

30
00:01:38,390 --> 00:01:38,730
Remember,

31
00:01:38,730 --> 00:01:44,850
I don't have to type any values after that, just ‑RootOnly is enough, and the

32
00:01:44,850 --> 00:01:49,030
command is designed to only show me vegetables that belong, that are root

33
00:01:49,030 --> 00:01:51,740
vegetables like carrots and turnips and potatoes,

34
00:01:51,740 --> 00:01:52,930
that sort of thing.

35
00:01:52,930 --> 00:01:59,240
Alright, so the help reinforced and told me how to use the command.

36
00:01:59,240 --> 00:02:03,190
Now I also know that the name parameter is positional,

37
00:02:03,190 --> 00:02:07,370
so I should be able to do Get‑Vegetable carrot, and that value

38
00:02:07,370 --> 00:02:11,680
carrot will be assigned to the name parameter, and sure enough,

39
00:02:11,680 --> 00:02:15,550
that works. Now some commands will have mandatory parameters. Let's

40
00:02:15,550 --> 00:02:18,440
look at help for New‑Vegetable.

41
00:02:18,440 --> 00:02:23,610
Now you can't quite tell it in the syntax there,

42
00:02:23,610 --> 00:02:29,580
so let's just quit this here, and I'm just going to do New‑Vegetable by itself.

43
00:02:29,580 --> 00:02:33,260
I didn't type in anything. PowerShell is saying,

44
00:02:33,260 --> 00:02:40,890
hey, New‑Vegetable has a parameter, ‑name, and it is required, it's mandatory,

45
00:02:40,890 --> 00:02:44,070
so it's prompting me to put in some value. See that

46
00:02:44,070 --> 00:02:47,360
little exclamation point, question mark?

47
00:02:47,360 --> 00:02:54,090
If you type that at the prompt there,

48
00:02:54,090 --> 00:02:56,610
if the command is written with a little help message,

49
00:02:56,610 --> 00:03:00,140
you can get that listed there, and then it'll tell you, hey,

50
00:03:00,140 --> 00:03:01,930
what is the name of the vegetable.

51
00:03:01,930 --> 00:03:06,370
So I'm going to go ahead and create, let's create okra.

52
00:03:06,370 --> 00:03:13,690
Oh, let's make it all proper lowercase here, okra. Okra is green,

53
00:03:13,690 --> 00:03:17,540
and I'm just going to make up a UPC code.

54
00:03:17,540 --> 00:03:20,260
Alright, so now I've created a new vegetable.

55
00:03:20,260 --> 00:03:26,340
I had the required parameter, but I was able to handle that just fine.

56
00:03:26,340 --> 00:03:28,670
If we go back and look at help for New‑Vegetable

57
00:03:28,670 --> 00:03:31,170
and look at that name parameter, sure enough,

58
00:03:31,170 --> 00:03:33,280
you can see that it is required.

59
00:03:33,280 --> 00:03:37,440
It's positional, so you don't have to specify the name parameter.

60
00:03:37,440 --> 00:03:44,500
There is no default value, and it doesn't accept wildcards.

61
00:03:44,500 --> 00:03:49,430
Let's look at one more example with my vegetable commands and parameters.

62
00:03:49,430 --> 00:03:53,140
Let's look at Set‑Vegetable.

63
00:03:53,140 --> 00:03:53,440
Alright,

64
00:03:53,440 --> 00:03:58,230
so you can see here there are two parameter sets to use this,

65
00:03:58,230 --> 00:04:01,050
one that allows you to do an input object,

66
00:04:01,050 --> 00:04:03,050
which means, generally when you see that,

67
00:04:03,050 --> 00:04:05,610
that means something is going to be piped into the command.

68
00:04:05,610 --> 00:04:10,320
Or you can specify modifying vegetables by parameters, you can see

69
00:04:10,320 --> 00:04:14,140
there's ‑name and parameters for changing the cooking state and

70
00:04:14,140 --> 00:04:17,430
the number of vegetables and whatnot.

71
00:04:17,430 --> 00:04:21,760
So let's just run help for Set‑Vegetable and look at

72
00:04:21,760 --> 00:04:24,420
all the parameters in more detail.

73
00:04:24,420 --> 00:04:27,780
So CookingState, you can see a little description there.

74
00:04:27,780 --> 00:04:29,190
You can see it's not required.

75
00:04:29,190 --> 00:04:30,400
It's named, which means,

76
00:04:30,400 --> 00:04:36,320
if I want to use that parameter I have to type ‑CookingState, count,

77
00:04:36,320 --> 00:04:39,320
input object, and so on, so you can look at those in

78
00:04:39,320 --> 00:04:42,400
a little more detail on your own.

79
00:04:42,400 --> 00:04:48,280
Let's go ahead and run Set‑Vegetable, and let's modify corn, and let's grill

80
00:04:48,280 --> 00:04:51,900
it, because I love grilled corn. And we'll use ‑passthru,

81
00:04:51,900 --> 00:04:57,110
which tells PowerShell, hey, show me the object that was written to

82
00:04:57,110 --> 00:05:01,230
the pipeline, and there is my updated object,

83
00:05:01,230 --> 00:05:04,370
which I can verify again by running Get‑Vegetable using the

84
00:05:04,370 --> 00:05:10,680
positional parameter corn, and there we go, so that all works. Even

85
00:05:10,680 --> 00:05:14,060
though I had no idea, and you have no idea how to use these commands,

86
00:05:14,060 --> 00:05:16,400
by taking the time to read the help,

87
00:05:16,400 --> 00:05:23,050
It will tell you what you can expect and how you can use the command.

88
00:05:23,050 --> 00:05:26,030
I know we're just getting started with PowerShell, but at least let me

89
00:05:26,030 --> 00:05:31,070
give you a taste of this idea of pipeline and object in the pipeline,

90
00:05:31,070 --> 00:05:35,120
because I want to make sure you can kind of understand what this looks

91
00:05:35,120 --> 00:05:38,800
like in terms of the help documentation.

92
00:05:38,800 --> 00:05:42,170
So if I look at my vegetable command and look at the name parameter

93
00:05:42,170 --> 00:05:47,700
again for Get‑Vegetable, we see, just to reiterate here,

94
00:05:47,700 --> 00:05:49,710
this will accept pipeline input,

95
00:05:49,710 --> 00:05:54,910
meaning I can send stuff into this command and it will take it by

96
00:05:54,910 --> 00:05:59,790
value or by property name. By value, again, means if I see

97
00:05:59,790 --> 00:06:02,740
something, a thing like a string or a word,

98
00:06:02,740 --> 00:06:07,780
I'm going to assume that must be the name of a vegetable. By property name

99
00:06:07,780 --> 00:06:14,390
means if I see an object, and if the object coming in has a name property, or

100
00:06:14,390 --> 00:06:20,030
to be clearer, a property called name, I will then take that value from the

101
00:06:20,030 --> 00:06:24,940
object and assign it to the name parameter, because the name parameter matches

102
00:06:24,940 --> 00:06:27,100
the name property on the object.

103
00:06:27,100 --> 00:06:28,630
Those two things go together,

104
00:06:28,630 --> 00:06:34,040
and so the values just kind of get piped and passed on through.

105
00:06:34,040 --> 00:06:37,800
So, let me show you this, now that you can kind of see that the help says,

106
00:06:37,800 --> 00:06:37,980
hey,

107
00:06:37,980 --> 00:06:43,280
this should work. So I should be able to get content on a list of vegetables

108
00:06:43,280 --> 00:06:49,010
that I have in my work folder and pipe them to Get‑Vegetable. PowerShell under

109
00:06:49,010 --> 00:06:51,430
the hood is going to go through that list and say,

110
00:06:51,430 --> 00:06:54,890
hey, I see the first value is carrot,

111
00:06:54,890 --> 00:06:57,930
so I'm going to assign that to the name parameter.

112
00:06:57,930 --> 00:07:01,320
I'll do that again for broccoli and then cauliflower,

113
00:07:01,320 --> 00:07:04,910
and so Get‑Vegetable gives me the results that I am expecting

114
00:07:04,910 --> 00:07:10,960
from reading in that list of vegetables from the text file. To

115
00:07:10,960 --> 00:07:16,140
make sure you really understand this, let's try something that's going to fail.

116
00:07:16,140 --> 00:07:21,480
So I'm just going to get a list of shares in a text file I have here,

117
00:07:21,480 --> 00:07:26,570
and I'm just going to save this to a variable $list so I don't have to rerun

118
00:07:26,570 --> 00:07:30,820
the Get‑Content, and just so you can see what is in $list.

119
00:07:30,820 --> 00:07:31,230
Alright,

120
00:07:31,230 --> 00:07:36,830
so I'm looking for shares on my computer called work, scripts, and reports.

121
00:07:36,830 --> 00:07:41,440
Now I have this command Get‑SMBshare, and we can look at help real quickly.

122
00:07:41,440 --> 00:07:48,050
You can see that there is a Name parameter. So I'm not even going to dig too

123
00:07:48,050 --> 00:07:50,730
far into it, let's just say I glanced at the help and go,

124
00:07:50,730 --> 00:07:57,370
okay, I should be able to maybe pipe $list to Get‑SMBshare,

125
00:07:57,370 --> 00:08:02,700
and I should be able to get all the shares that match those names in that list.

126
00:08:02,700 --> 00:08:04,050
No I don't.

127
00:08:04,050 --> 00:08:07,260
I get a bunch of error messages. Now, just so you know,

128
00:08:07,260 --> 00:08:12,360
I changed the color of the background error message so that

129
00:08:12,360 --> 00:08:15,230
it would be easier to see on the screen.

130
00:08:15,230 --> 00:08:18,020
Get‑SMBshare is giving me errors that says,

131
00:08:18,020 --> 00:08:18,740
hey,

132
00:08:18,740 --> 00:08:23,530
the input object cannot be mapped or bound to the parameters of any command,

133
00:08:23,530 --> 00:08:27,100
and I get that three times because it's trying to do that for each thing

134
00:08:27,100 --> 00:08:30,340
that is getting piped in. And I may be thinking,

135
00:08:30,340 --> 00:08:31,840
well, why doesn't that work?

136
00:08:31,840 --> 00:08:36,440
I mean, it worked for Get‑Vegetable, why doesn't it work for Get‑SMBshare?

137
00:08:36,440 --> 00:08:38,580
This is why you need to read the help.

138
00:08:38,580 --> 00:08:42,520
So I go back now and look at the Name parameter.

139
00:08:42,520 --> 00:08:48,180
You can see that it will take names as a collection of strings,

140
00:08:48,180 --> 00:08:52,090
but it does not accept pipeline input by value,

141
00:08:52,090 --> 00:08:53,550
which is what I was trying to do.

142
00:08:53,550 --> 00:08:56,720
It will take it by property names.

143
00:08:56,720 --> 00:09:03,010
So what that means is if I were to do import csv, and if I have a CSV file that

144
00:09:03,010 --> 00:09:10,970
has the names of the shares, then that I can pipe into Get‑SMBshare and that

145
00:09:10,970 --> 00:09:15,100
works, and I get an error for reports because that share does not exist on the

146
00:09:15,100 --> 00:09:19,360
computer, and that's kind of what I want to see here anyway. You can take a

147
00:09:19,360 --> 00:09:25,910
look at data.csv if you want to see how that file is structured. Import csv is

148
00:09:25,910 --> 00:09:29,550
reading the CSV file and creating an object with the header names as the

149
00:09:29,550 --> 00:09:30,940
property names.

150
00:09:30,940 --> 00:09:33,660
There is a property called Name,

151
00:09:33,660 --> 00:09:39,150
so PowerShell says, hey, this command can take input by property name, and

152
00:09:39,150 --> 00:09:43,620
so I'll take that name property of the incoming object, assign it to the

153
00:09:43,620 --> 00:09:47,240
parameter value, and then the magic happens.

154
00:09:47,240 --> 00:09:49,680
That is the power of the pipeline,

155
00:09:49,680 --> 00:09:53,180
and you may need to refer to the PowerShell help to understand

156
00:09:53,180 --> 00:09:57,490
why it may or may not be working in your work or the things

157
00:09:57,490 --> 00:10:03,000
that you are trying to get done. So with that, let's go back to the slides and wrap up this lesson.

