1
00:00:01,040 --> 00:00:04,760
So let me give you some basics on working with script blocks.

2
00:00:04,760 --> 00:00:09,700
Now in this module, all of my code samples are going to be in the slides,

3
00:00:09,700 --> 00:00:11,890
I'm not really going to be any live demo because it's just

4
00:00:11,890 --> 00:00:14,340
as easy to see them on the screen here,

5
00:00:14,340 --> 00:00:17,880
but the course downloads does have all of the code that I'm showing you,

6
00:00:17,880 --> 00:00:20,150
so you can download that, grab the code,

7
00:00:20,150 --> 00:00:23,840
open it up in your editor, and try these things out for yourself.

8
00:00:23,840 --> 00:00:25,970
So I'm going to create a script block and I'm going

9
00:00:25,970 --> 00:00:28,020
to save this to a variable $sb.

10
00:00:28,020 --> 00:00:30,160
In the script block,

11
00:00:30,160 --> 00:00:35,070
we're going to have a set of curly braces and whatever code that you want

12
00:00:35,070 --> 00:00:39,940
to run in the script block goes inside the curly braces.

13
00:00:39,940 --> 00:00:43,840
Inside the curly braces, you can have a single line of code as I have here,

14
00:00:43,840 --> 00:00:45,480
you can have multiple lines of code,

15
00:00:45,480 --> 00:00:48,210
just hit Enter after each line and then just make sure

16
00:00:48,210 --> 00:00:50,320
you close your closing curly brace.

17
00:00:50,320 --> 00:00:53,740
PowerShell will figure out what is inside your

18
00:00:53,740 --> 00:00:56,940
script block and know how to run it.

19
00:00:56,940 --> 00:01:02,000
You can have as much code as you want inside those outer curly braces.

20
00:01:02,000 --> 00:01:06,390
If it's a single line like I have here, you really don't have to do anything.

21
00:01:06,390 --> 00:01:08,120
If you have multiple lines of code,

22
00:01:08,120 --> 00:01:11,470
just hit Enter after every line and then PowerShell will figure

23
00:01:11,470 --> 00:01:15,640
it out when you try to use that script block.

24
00:01:15,640 --> 00:01:18,500
To run the script block like that one I just created,

25
00:01:18,500 --> 00:01:22,340
you can use the invoke operator which is the ampersand here,

26
00:01:22,340 --> 00:01:24,740
so I can just tell PowerShell ampersand,

27
00:01:24,740 --> 00:01:29,720
meaning run or invoke, the variable $sb because that's a script block,

28
00:01:29,720 --> 00:01:31,510
PowerShell knows what to do.

29
00:01:31,510 --> 00:01:34,840
So the script block runs and I get the results.

30
00:01:34,840 --> 00:01:38,480
The output of the script block is the same as if I had

31
00:01:38,480 --> 00:01:41,010
manually run the code within the script block.

32
00:01:41,010 --> 00:01:43,000
The difference is the script block made it a whole

33
00:01:43,000 --> 00:01:45,840
lot easier for me to run that code.

34
00:01:45,840 --> 00:01:49,600
If I decided later in the day I want to get the same data again,

35
00:01:49,600 --> 00:01:53,200
all I have to do is rerun that really short script block variable,

36
00:01:53,200 --> 00:01:56,900
I don't have to try to rerun all the code that is inside the

37
00:01:56,900 --> 00:01:59,040
script block that I created originally.

38
00:01:59,040 --> 00:02:02,840
This obviously makes PowerShell really easy to reuse.

39
00:02:02,840 --> 00:02:06,920
The other way that you can invoke a script block is to use Invoke‑Command.

40
00:02:06,920 --> 00:02:08,580
Invoke‑Command, if you looked at the help,

41
00:02:08,580 --> 00:02:11,840
you'll see has a script block parameter which is positional,

42
00:02:11,840 --> 00:02:15,730
so I can just do Invoke‑Command or the alias icm and

43
00:02:15,730 --> 00:02:18,520
give it the name of my variable, which has a script block,

44
00:02:18,520 --> 00:02:20,640
and PowerShell will do its thing,

45
00:02:20,640 --> 00:02:23,940
and I'll get the same result as I just showed you.

46
00:02:23,940 --> 00:02:27,380
You'll also find Invoke‑Command very useful when you go to

47
00:02:27,380 --> 00:02:30,700
PowerShell remoting because I'll be able to run that script block

48
00:02:30,700 --> 00:02:34,840
remotely on one or more multiple computers.

49
00:02:34,840 --> 00:02:36,060
So as I said earlier,

50
00:02:36,060 --> 00:02:39,700
script blocks are everywhere in PowerShell used all the time,

51
00:02:39,700 --> 00:02:43,140
you've seen them, you may not have known what they were called.

52
00:02:43,140 --> 00:02:45,890
So Get‑Process, piping it to Where‑Object.

53
00:02:45,890 --> 00:02:50,800
Where‑Object is using a filtering script block so that code within this

54
00:02:50,800 --> 00:02:56,170
curly braces where $_.ws is greater equal to 100 MB,

55
00:02:56,170 --> 00:03:01,750
PowerShell evaluates or runs that code, filters the process objects,

56
00:03:01,750 --> 00:03:07,140
and then I can pipe them to select object and there are the results.

57
00:03:07,140 --> 00:03:08,740
Here is another example.

58
00:03:08,740 --> 00:03:14,110
I am first defining a hashtable called $new and this will be

59
00:03:14,110 --> 00:03:17,320
parameters that are part of the new item cmdlet which is

60
00:03:17,320 --> 00:03:19,640
going to be in my foreach object.

61
00:03:19,640 --> 00:03:22,090
So I'm going to take the numbers 1 through 10,

62
00:03:22,090 --> 00:03:24,800
and for each 1 of those numbers,

63
00:03:24,800 --> 00:03:30,410
I'm going to create a new directory called Data‑1‑2‑3 and

64
00:03:30,410 --> 00:03:34,240
so on and I'm splatting the hashtable.

65
00:03:34,240 --> 00:03:37,390
So for each object is using a script block,

66
00:03:37,390 --> 00:03:41,740
so you have the curly brace New‑Item code closing curly brace.

67
00:03:41,740 --> 00:03:53,000
Don't confuse the hashtable that I've defined with $new with a script block. The hashtable has the @ symbol in front of it, the script block does not.

