Understanding PowerShell Scriptblocks

Scriptblocks are a ubiquitous type of construct in PowerShell. In this video, we’re going to cover everything from creating scriptblocks, executing scriptblocks with the call operator, using Invoke-Command to run scriptblocks and even going over some things you need to look out for around variable scoping.
And this step, I'd like to go over understanding PowerShell script blocks. So to understand powerShell script blocks. You really have to understand code expressions and functions so script blocks are actually anonymous function. If they don't have a name. They provide a very similar purpose in powershell, but they are a little bit different. You'll see a lot of these similarities in this snip, so first off to create a script block. We just simply need to enclose some amount of code in this instance, I'm just doing 1 + 1. Could be any amount of code in here? We enclose that with curly braces. Once we create the script block. We can then check and see what type it is by using the gettype method and the full name property and we can see that it is system dot management dot automation dot script lock. And then to call that script block, we have a few different ways, 1st way we can use the call operator or the ampersand. We can put the ampersand in front of the script block and run that and then you can see that the code inside of that script block is ran so that's 1 way can do it next way we can use the invoke command commandlet the invoke command commandlet is typically ran on remote computers, but you can also run it on a local computer as well. You simply need to pass the script block to the block parameter and it will run on the. Local computer just as you would with the call operator. Next up, we have scriptblock parameter, so just like functions. A script block and have one or more different parameters in this case. I am creating a parameter block an aside there. I have 2 parameters string and number and then after the Param block. I have my code. Wherever I'm going to execute in this case. I'm just going to add one to the number variable and then output. The string just to console. Just very simple just to show you that it is passing in those values so I can go ahead and create this. And then I can execute this with the call operator and then notice that it added one to the number and then output Foo as the string, unlike functions, though. You have to pass parameters to a scriptblock via positional parameters, so notice that on line 14. I'm not defining the parameter name itself. I'm just providing the value. PowerShell knows which value which argument to pass to the parameter based on the position so fu is first so it automatically knows it's going to be string and then the number of 3 is next, which automatically knows it's going to be number. And then you can pass parameters with the invoke command commandlet as well. By using the argument list parameter. So same difference. The value that you're using for argument list. Here is the same as the values are passing above for the call operator. All right, so Next up? Is variables in script blocks, so let's define our variable and then it's very simple. You can see that in my script block here. I'm just I have a variable inside of the script block and then when I run it with a call operator or with invoke command. If I choose it expands correctly the same scope. It understands what the value of the variable is and there are some cases where you this may not work because of scoping issues. I'll show you a little bit about scoping in a minute, but you're on line 24. You'll find that sometimes it doesn't work. You need to expand the variable out first and then pass this script block to a remote computer of in this instance, I can use the scriptblock object itself and use the create method on here, which then creates the scriptblock an expands that variable inside so it statically in there, I could do that as well. Now that I know that I don't worry about scoping the value is actually stored inside of the script lock. Next step want to talk about scoping a little bit so let me run. This command here and I'll show you what this looks like. OK, so you'll see that the behavior scriptblock you won't run into this a whole lot. But there are sometimes when you will so on in this scoping section here. I'm setting I to 2 and then I'm putting the value of I in the script block, but outside of the scriptblock. I'm changing the value of I to 3 and then notice that whenever I execute it 3. Is there so 3 is in the script block so the value itself is not inside of the scriptblock natively. It's a reference variable so whenever you change the? Original variable I in this case any case in any instance outside of the scriptblock itself, it will actually change the value in the script block, but let's say that you want to keep the value inside of there to do that. You can then use the new enclosure method. So I will just run this so now notice that I have equals 2 and I equals 3 same exact thing and then I have all the same code except I'm using get new enclosure there. I'm calling the get new enclosure method. This actually creates a copy of the script block and then saves the value of I. Whatever the variable is there inside of the script block and then now you notice that it says the value of I is two because I can change the value of I anytime that I want outside of the script block and it will always remain the same, so let's just say I change it to. I equals 5 and then I check this out. It's still the value of I equals 2 so unless I call get new enclosure. The value that is your dollar. I hear will change get new enclosure is a method that creates that copy which thing keeps that static inside of there. OK, so that has been a snip on understanding PowerShell script blocks, thanks for watching.