1
00:00:01,040 --> 00:00:03,010
Script blocks are really cool things.

2
00:00:03,010 --> 00:00:06,740
Not only can you save a block of code and rerun it easily,

3
00:00:06,740 --> 00:00:09,670
you can also add parameters to your script block,

4
00:00:09,670 --> 00:00:11,440
and this is what it looks like.

5
00:00:11,440 --> 00:00:16,240
So I have a script block, $sb, that's going to basically run Get‑WinEvent,

6
00:00:16,240 --> 00:00:22,100
but I want to be able to pass to the script block the log name that I want to

7
00:00:22,100 --> 00:00:27,240
query, as well as the number of events that I want to get.

8
00:00:27,240 --> 00:00:32,640
So we're going to add a Param block at the beginning of the script block.

9
00:00:32,640 --> 00:00:36,540
So after the opening curly brace you can put in Param, and then in

10
00:00:36,540 --> 00:00:41,950
parentheses a comma‑separated list of your parameter. So the parameter

11
00:00:41,950 --> 00:00:46,960
will be a variable, and we'll use that variable in the code. So I've got

12
00:00:46,960 --> 00:00:51,310
$log and $count as the parameters, and you can see that they are used as

13
00:00:51,310 --> 00:00:54,150
$log and $count in the code.

14
00:00:54,150 --> 00:00:57,130
It's very important that you understand that parameters are all

15
00:00:57,130 --> 00:01:00,050
positional, so when we go to use parameters,

16
00:01:00,050 --> 00:01:03,420
you really have to specify all of them and you have

17
00:01:03,420 --> 00:01:05,620
to do them all in the same order.

18
00:01:05,620 --> 00:01:11,840
We can't specify them by name, it's just, as you'll see, it's just by value.

19
00:01:11,840 --> 00:01:15,790
So the way that this works then, because they are by value,

20
00:01:15,790 --> 00:01:17,860
you would invoke the script block,

21
00:01:17,860 --> 00:01:22,500
so there's the &$sb, and then specify all of the

22
00:01:22,500 --> 00:01:24,400
parameter values that you want to pass.

23
00:01:24,400 --> 00:01:32,040
So I'm passing system 2, so system will go to log and 2 will go to count,

24
00:01:32,040 --> 00:01:39,320
and Get‑WinEvent will then run Get‑WinEvent ‑log system ‑maximum count 2,

25
00:01:39,320 --> 00:01:45,420
and then that output will be sent to the pipeline and go on to Format‑List

26
00:01:45,420 --> 00:01:48,720
to format the display for me. As you see,

27
00:01:48,720 --> 00:01:52,340
you can use the invoke operator, the ampersand, and the

28
00:01:52,340 --> 00:01:54,640
parameters are separated by spaces.

29
00:01:54,640 --> 00:01:57,040
There's no commas, we don't name anything,

30
00:01:57,040 --> 00:02:01,070
you just specify the value, and here's the result that you can get.

31
00:02:01,070 --> 00:02:06,340
So again, I've been able to use that script block to simplify running my code.

32
00:02:06,340 --> 00:02:10,460
If I want to check a different event log or get a different number of events,

33
00:02:10,460 --> 00:02:16,040
I can easily rerun the script block and just specify different parameter values.

34
00:02:16,040 --> 00:02:18,000
If you are using Invoke‑Command,

35
00:02:18,000 --> 00:02:20,310
especially when you get to doing stuff with remoting,

36
00:02:20,310 --> 00:02:24,870
you're going to use the ‑ArgumentList parameter, and here you are going to

37
00:02:24,870 --> 00:02:29,900
separate the parameter values for your script block by commas.

38
00:02:29,900 --> 00:02:34,840
So here I've got System,2, so again, they're all positional.

39
00:02:34,840 --> 00:02:35,960
When you get to remoting,

40
00:02:35,960 --> 00:02:40,080
you'll be using Invoke‑Command quite a bit, and being able to specify different

41
00:02:40,080 --> 00:02:42,960
arguments for your script blocks is kind of a handy thing.

42
00:02:42,960 --> 00:02:46,970
Keep in mind, you have to specify all of the parameters, and

43
00:02:46,970 --> 00:02:50,480
in order, so I try to keep the number of parameters I need

44
00:02:50,480 --> 00:02:52,060
in the script block limited.

45
00:02:52,060 --> 00:02:54,980
There are other ways that you can pass values into a

46
00:02:54,980 --> 00:02:56,650
script block with Invoke‑Command,

47
00:02:56,650 --> 00:03:01,000
but that's kind of outside the scope of what we're going to cover in this course.

