1
00:00:01,140 --> 00:00:03,720
Looking ahead in your PowerShell future,

2
00:00:03,720 --> 00:00:07,080
there are other ways that you will be able to use script blocks.

3
00:00:07,080 --> 00:00:09,150
We're not covering them in this course.

4
00:00:09,150 --> 00:00:12,000
There are other courses in the Pluralsight library you'll find

5
00:00:12,000 --> 00:00:14,140
that will explain some of these features,

6
00:00:14,140 --> 00:00:16,740
but I wanted to give you a taste of what they look like.

7
00:00:16,740 --> 00:00:22,440
PowerShell has the ability to run commands in the background using a job system.

8
00:00:22,440 --> 00:00:26,630
The easy way to use this is Start‑Job, and you can specify a script block.

9
00:00:26,630 --> 00:00:30,140
The script block, if you need to, can also include parameters.

10
00:00:30,140 --> 00:00:35,790
So here, I am basically running my same Get‑WinEvent command in the script block,

11
00:00:35,790 --> 00:00:39,020
but I'm also grouping the results, and sorting results,

12
00:00:39,020 --> 00:00:42,540
and using the ArgumentList for Start‑Job to pass some

13
00:00:42,540 --> 00:00:47,340
parameters to the script block, which then runs in the background.

14
00:00:47,340 --> 00:00:49,640
When the job is finished,

15
00:00:49,640 --> 00:00:53,490
I can use Receive‑Job and retrieve the information and then format,

16
00:00:53,490 --> 00:00:56,540
in this case, the results as a table.

17
00:00:56,540 --> 00:01:01,270
This is just another example of how script blocks are used often in PowerShell.

18
00:01:01,270 --> 00:01:03,840
And when you get to scripting,

19
00:01:03,840 --> 00:01:07,560
a function really is nothing more than a named script block.

20
00:01:07,560 --> 00:01:11,780
So the script block here is the same script block that I used in my job.

21
00:01:11,780 --> 00:01:14,000
The time, I've given it a name, Get‑LogInfo,

22
00:01:14,000 --> 00:01:16,640
and turned it actually into a function.

23
00:01:16,640 --> 00:01:19,320
Once this function is loaded into my PowerShell session,

24
00:01:19,320 --> 00:01:22,640
I can just run GetLogInfo, and the command will run.

25
00:01:22,640 --> 00:01:28,140
Script blocks are everywhere in PowerShell.

26
00:01:28,140 --> 00:01:31,640
Once again, let's summarize what we've covered in this brief module.

27
00:01:31,640 --> 00:01:36,340
Script blocks are used often and everywhere in PowerShell,

28
00:01:36,340 --> 00:01:38,340
either in cmdlets, functions,

29
00:01:38,340 --> 00:01:42,540
or things that you will write just to use ad hoc at the console.

30
00:01:42,540 --> 00:01:45,040
They're treated as units of code,

31
00:01:45,040 --> 00:01:50,160
makes it easier to rerun a block of code by just referencing a variable,

32
00:01:50,160 --> 00:01:55,140
or when you get scripting a function, Script blocks can use parameters,

33
00:01:55,140 --> 00:01:57,070
just remember that they are positional.

34
00:01:57,070 --> 00:02:00,390
Script blocks are no different than any other command in PowerShell.

35
00:02:00,390 --> 00:02:04,400
They write objects to the pipeline, and you'll want to write script blocks that,

36
00:02:04,400 --> 00:02:06,840
in fact, will do that.

37
00:02:06,840 --> 00:02:08,950
You can create your own for ad hoc work.

38
00:02:08,950 --> 00:02:10,110
I do this all the time.

39
00:02:10,110 --> 00:02:12,200
If I'm running something and I want to, hey,

40
00:02:12,200 --> 00:02:14,710
and I need, I'm going to need to rerun this later in the day,

41
00:02:14,710 --> 00:02:16,720
I'll just create a little quick script block, and

42
00:02:16,720 --> 00:02:18,940
then I can rerun that script block.

43
00:02:18,940 --> 00:02:21,080
If it's something that I realize, you know what, I'm

44
00:02:21,080 --> 00:02:26,140
going to need this all the time, then I can start turning that into a function.

45
00:02:26,140 --> 00:02:28,540
Don't focus on just the fact that, oh,

46
00:02:28,540 --> 00:02:30,410
that's a script block, that's a script block, just

47
00:02:30,410 --> 00:02:32,870
recognize what a script block is,

48
00:02:32,870 --> 00:02:36,470
but don't get hung up on thinking, I have to write a script block. You don't.

49
00:02:36,470 --> 00:02:41,350
I just took time in this particular module to demonstrate and define what a

50
00:02:41,350 --> 00:02:43,940
script block is so that when you see that term,

51
00:02:43,940 --> 00:02:47,480
you'll know what PowerShell is talking about. And as a reminder,

52
00:02:47,480 --> 00:02:50,520
all of the code samples that I used in the slides are available

53
00:02:50,520 --> 00:02:51,650
as part of the course downloads,

54
00:02:51,650 --> 00:02:56,940
so you can load them up in your PowerShell editor and try them out for yourself.

55
00:02:56,940 --> 00:02:59,630
We've reached the end of another module. So again,

56
00:02:59,630 --> 00:03:01,410
time for you to stretch your legs,

57
00:03:01,410 --> 00:03:08,000
get some fresh air, and come back, and we will continue on learning more about the PowerShell language.

