0:00
What's up everyone? Today we're going to talk about how you can process pipeline input into your custom scripts or functions using begin, process, and end blocks
0:10
This is a really great feature that you can use and then you can also use it to process multiple parameter values for your script or function
0:18
Let's go ahead and get started. First, let's take a look at our example function here. It's just called showBeginProcessEnd
0:25
On line two we have our cmdlet binding attribute just giving us compiled cmdlet functionality inside of our function here
0:32
I have just one parameter and you'll notice in the parameter attribute it is mandatory
0:37
Then we have another attribute here called valueFromPipeline. It's just an integer and the name of that parameter is item
0:45
Then we have three blocks here of code with the keywords begin, process, and end
0:51
Inside each of those is the code that belongs to each of those sections
0:55
I believe you don't have to define begin and end. I think you can get away with just doing process
1:01
We're going to show an example here of all three of them. Each block executes in a certain order. Obviously begin is first, process is the middle, and end is last
1:11
The begin block will execute only once right at the very beginning
1:15
The process block will process however many times it needs to. Then the end block is at the very end right as the scripter function exits
1:24
As it sits right now, let's go ahead and take a look at what this looks like
1:29
I'm going to highlight all this and press F8 to bring that function into my current section here
1:35
What we'll do is just pass in several numbers into it. We can do that quite easily doing this type of syntax, 1.5
1:46
That will just output the numbers 1 through 5. I can take that and I'm going to pipe it to my custom function here, show begin, process end
1:57
Let's take a look at this output here. It does exactly what we should hope it would do
2:03
We see this is the begin block, statement out. We show that it is processing item, dollar sign item
2:09
That's our parameter value. We see it does it five times because we passed it five numbers
2:14
Then once at the very end, this is the end block. Perfect. This did exactly what we had hoped
2:20
It took pipeline values of the numbers 1 through 5, ran it through a function
2:25
Notice we didn't even have to do anything here in the process block of a for each loop or anything like that
2:31
It just knew that I am processing input from the pipeline in my process block here
2:38
and just did it automatically. In addition to taking input like this, it can also be a variable
2:44
If we were to do something like enter ray equals 1 through 5
2:50
Here's enter ray. If we output that again, it's just the numbers 1 through 5
2:55
We can take enter ray and pass it to our function here
3:00
It gives us the exact same thing. This is because it is able to match up
3:06
It sees the enter ray has integers in it and it matches it to our item parameter
3:12
Let's show how we can use the begin and end blocks just a little bit differently to do exactly that
3:18
Maybe set up some things that we need for the function and then at the end of it use the end block to output that
3:24
I'm going to erase this here and we'll go to a new line
3:28
Let's say we want to add these numbers up, but we want to make sure we start at 0, that we don't have anything in there
3:34
Then we are going to put in a counter to see how many things we added up
3:39
We have two variables here. We're going to leave our processing item just so we can see that output and how it processes what we're passing in from the pipeline
3:48
We're going to do sum plus equal the current item. Then we're going to add one to our count each time it processes something from the pipeline just to keep track of how many things we are processing
4:04
Then at the very end we're just going to output some information here
4:09
We'll say the sum of the items is sum. Then the total number of items is count
4:23
Format all of that. Perfect. Again, in our begin block we're setting up things that we need for our function to process
4:31
We're zeroing out sum and count. Then in our process block we're processing through each item
4:36
We're adding to our sum of the current item value and then adding how many things we're adding
4:41
At the end we're outputting our results here. Again, get this new version of our function in here
4:48
Let's go back up and let's take our int array and pass it into our function here
4:54
Now we see processing 1, 2, 3, 4, 5 which is what we're expecting
4:59
At the end the sum of the items is 15 and the total number of items is 5
5:04
We didn't have any output or anything going on in our begin block but we know it's still executed because if we were to run this again it's not continually adding on top of them
5:15
It only executed once just to zero those out and get us going there
5:19
Now this function as we have it right now is great for passing in values from the pipeline, multiple values even
5:27
But our parameter here $item, is it capable of processing multiple items
5:33
Let's take a look and see what happens. Let's just call showBeginProcessIn. We'll give it an item
5:39
Let's just give it one item. We'll give it the number 5. That's what we expected. It only had one thing to process
5:46
It's total sum is 5 and we only had one count. Pretty straightforward
5:51
But what if we were to try to pass in an array of items here like we did before
5:58
We'll put this in parentheses and let's say we want to do the same thing. We want to add up 1 through 5 here
6:06
This is going to give us an error. It's going to say the parameter $item, we can't take this object that we have, the 1 through 5, and add it onto each other
6:17
Basically it's having an issue on line 17 because it can't take 0, which is an integer, and try to add this object to it
6:26
That's because our process block can only natively process things that are coming in from the pipeline
6:34
In this case we're passing in our list or array of values in as our parameter, which is only supposed to be a single integer
6:43
But we can make a couple of changes here in order to accommodate either multiple pipeline values or multiple parameter values
6:51
We have to come in here and we'll change this to an array of integers by adding some more square brackets there
6:59
Go ahead and save that change. However, our process block, as I just mentioned, only works on pipeline values
7:07
It doesn't work on multiple parameter values. Because when the process block is going through and processing pipeline values, it's only taking one value at a time
7:18
It doesn't see all the values coming in. It just goes, oh, there's one value in the pipeline that's coming next, and then another one, then another one
7:26
It doesn't know how many necessarily there are. In order to accommodate both of these scenarios, we need to come into our process block
7:34
This is where we actually add a foreach block. We'll say foreach in item
7:44
Then we'll take each of these in here. Let's take this out and put our logic here inside of our foreach loop
7:55
Now we then need to change our current item to just a lowercase i and save that
8:06
What we're doing here is the process block will still iterate through multiple pipeline objects
8:12
but our foreach is going to iterate through multiple parameter values if we call it not using values from the pipeline
8:20
We go ahead and highlight all of this, and we'll bring in our current version here of our function
8:25
Now let's go back to running this version where we're passing in multiple values into our parameter
8:33
Now it executes as expected. We're processing 1 through 5. The sum of these items is 15, and the total number is 5, what we expected
8:44
Now you might be wondering why we had to add the foreach
8:50
As we already talked about, it's to process multiple items that are coming in through our item parameter
8:56
We also had to change our variable type on our item parameter to be an array of integers instead of just a single integer
9:04
I'm going to add a little bit of code here to show you how the process block is actually processing the incoming data
9:13
Let's go here and let's just say what is our current input, and we'll do $item
9:21
Let's update this function in our current session. Now let's go back, we'll do 1 through 5, show begin process end, and see its output
9:35
This is what I was talking about earlier where it is processing a single value from the pipeline one at a time
9:41
As we enter the process block here, and on line 16 is what we see first where input is just the number 1
9:49
because that's one value from the pipeline, it processed it. Our next input is 2, 3, 4, 5, and it's only processing those one at a time
9:59
Now the foreach still works because $item is just one item. You can do a foreach on just one thing
10:07
That's perfectly fine. It's normally made to do it on multiple things, but you can do just one thing
10:12
On line 17 when we're saying foreach i in $item, $item at this point is just one number, which is perfectly fine
10:19
We process it and it continues on. Now let's leave this up here and let's look at our other version where we're passing in multiple values into the parameter
10:31
and let's compare their output. Here our input is 1, 2, 3, 4, 5, so it has entered the process block
10:44
and the $item value is all of our values at once, and then it iterates through those in the foreach, and then gives us our output
10:53
In the example up here where we're taking in values from the pipeline
10:58
it runs through the process block five different times. When we give it multiple values through a parameter, it is only hitting that process block once
11:09
and then it's the power of the foreach loop that actually iterates through each value in order to add them up for us
11:17
I remember the first time I tried to do something like this, I thought it was a little bit confusing
11:21
so hopefully this example and seeing the output this way will help you figure out how this works exactly
11:28
If you're only concerned about taking input from the pipeline, then you don't need to do the foreach or change your data type on your parameter here
11:38
but if you want to be able to do both, either take multiple pipeline values or take multiple parameter values
11:45
this is how you have to do it. It almost seems a little bit redundant, but hopefully once you see how it's working here with this example and the output that we see
11:53
that it's unfortunately necessary. It seems like a little bit extra code, but that is what it is
11:59
That's just how it works, unfortunately. That does it for this video
12:03
Hopefully you can see how adding begin process and end blocks can help you process pipeline input
12:09
and then a couple of extra changes that you need to make if you want to process multiple values directly on the parameter itself
12:16
Thank you for watching, and we'll see you next time.