Script blocks are really cool things.
Not only can you save a block of code and rerun it easily,
you can also add parameters to your script block,
and this is what it looks like.
So I have a script block, $sb, that's going to basically run Get‑WinEvent,
but I want to be able to pass to the script block the log name that I want to
query, as well as the number of events that I want to get.
So we're going to add a Param block at the beginning of the script block.
So after the opening curly brace you can put in Param, and then in
parentheses a comma‑separated list of your parameter. So the parameter
will be a variable, and we'll use that variable in the code. So I've got
$log and $count as the parameters, and you can see that they are used as
$log and $count in the code.
It's very important that you understand that parameters are all
positional, so when we go to use parameters,
you really have to specify all of them and you have
to do them all in the same order.
We can't specify them by name, it's just, as you'll see, it's just by value.
So the way that this works then, because they are by value,
you would invoke the script block,
so there's the &$sb, and then specify all of the
parameter values that you want to pass.
So I'm passing system 2, so system will go to log and 2 will go to count,
and Get‑WinEvent will then run Get‑WinEvent ‑log system ‑maximum count 2,
and then that output will be sent to the pipeline and go on to Format‑List
to format the display for me. As you see,
you can use the invoke operator, the ampersand, and the
parameters are separated by spaces.
There's no commas, we don't name anything,
you just specify the value, and here's the result that you can get.
So again, I've been able to use that script block to simplify running my code.
If I want to check a different event log or get a different number of events,
I can easily rerun the script block and just specify different parameter values.
If you are using Invoke‑Command,
especially when you get to doing stuff with remoting,
you're going to use the ‑ArgumentList parameter, and here you are going to
separate the parameter values for your script block by commas.
So here I've got System,2, so again, they're all positional.
When you get to remoting,
you'll be using Invoke‑Command quite a bit, and being able to specify different
arguments for your script blocks is kind of a handy thing.
Keep in mind, you have to specify all of the parameters, and
in order, so I try to keep the number of parameters I need
in the script block limited.
There are other ways that you can pass values into a
script block with Invoke‑Command,
but that's kind of outside the scope of what we're going to cover in this course.