1
00:00:01,040 --> 00:00:04,160
The last part of ForEach‑Object that I want to demonstrate to

2
00:00:04,160 --> 00:00:09,210
you is this parallel feature. This is not something that you

3
00:00:09,210 --> 00:00:10,890
necessarily need to use all the time,

4
00:00:10,890 --> 00:00:15,440
but I want you to understand how it works and why you might want to use it.

5
00:00:15,440 --> 00:00:18,590
I'm going to create a variable that's going to hold the names of

6
00:00:18,590 --> 00:00:22,440
four servers in my little test domain here.

7
00:00:22,440 --> 00:00:26,230
Now this is the way you would traditionally have done this in PowerShell.

8
00:00:26,230 --> 00:00:28,940
I'm going to save the results to a variable.

9
00:00:28,940 --> 00:00:32,370
You would say I'm going to take my variable with all those server

10
00:00:32,370 --> 00:00:42,110
names, and for each one I'm going to use Get‑WinEvent and get the

11
00:00:42,110 --> 00:00:46,680
security log, because I have to do this one at a time and get me

12
00:00:46,680 --> 00:00:50,840
the 2,500 most recent events.

13
00:00:50,840 --> 00:00:51,960
So I'm going to run this.

14
00:00:51,960 --> 00:00:57,430
This may take a little bit of time. So Get‑WinEvent can only process one

15
00:00:57,430 --> 00:01:01,290
computer at a time. And that ran relatively quickly.

16
00:01:01,290 --> 00:01:05,900
Let's see how quickly it ran. So I'm going to do get‑history ‑count 1,

17
00:01:05,900 --> 00:01:09,710
and this is going to give me the last command that I just ran. In

18
00:01:09,710 --> 00:01:14,130
PowerShell 7, this now has this little duration object. And I can

19
00:01:14,130 --> 00:01:18,930
see there that this ran in just a little over 6 seconds. And this

20
00:01:18,930 --> 00:01:22,360
should have got me, let's say measure the results, alright so I got

21
00:01:22,360 --> 00:01:25,640
10,000 entries.

22
00:01:25,640 --> 00:01:28,440
Now, let's try this in parallel.

23
00:01:28,440 --> 00:01:34,040
Let's take my same servers, and let's do foreach‑object.

24
00:01:34,040 --> 00:01:37,840
Now let's see that ‑parallel.

25
00:01:37,840 --> 00:01:42,300
Same command, Get‑WinEvent, Security, ‑ComputerName,

26
00:01:42,300 --> 00:01:48,040
‑MaxEvents, and have this run.

27
00:01:48,040 --> 00:01:49,960
This should run faster.

28
00:01:49,960 --> 00:01:51,880
It looks like it ran a little bit faster.

29
00:01:51,880 --> 00:01:56,240
Let's check the history here.

30
00:01:56,240 --> 00:01:58,080
Yep, so that took about 3 seconds.

31
00:01:58,080 --> 00:02:00,440
That took 50% of the time.

32
00:02:00,440 --> 00:02:05,240
Your results will vary, and depending on the command you're doing,

33
00:02:05,240 --> 00:02:06,690
sometimes it runs faster.

34
00:02:06,690 --> 00:02:10,400
Sometimes it may not because there could be other things that could be involved.

35
00:02:10,400 --> 00:02:14,530
It's very possible that the time that I'm running this in parallel,

36
00:02:14,530 --> 00:02:17,220
that maybe one of my servers is under high utilization

37
00:02:17,220 --> 00:02:21,390
that's really slow to respond. So, don't assume the

38
00:02:21,390 --> 00:02:24,060
‑parallel always means that it's faster,

39
00:02:24,060 --> 00:02:29,620
but often it can be. Because what's happening in the first scenario,

40
00:02:29,620 --> 00:02:32,920
Get‑WinEvent has to run for the first server,

41
00:02:32,920 --> 00:02:34,710
wait for all those results to come back,

42
00:02:34,710 --> 00:02:37,940
then it does the next one, then it does the next one.

43
00:02:37,940 --> 00:02:43,200
Whereas in parallel, it does all five of those at once, all at the same time.

44
00:02:43,200 --> 00:02:45,030
If there's more than five,

45
00:02:45,030 --> 00:02:48,680
then it adds the next one to the list as each of them finished.

46
00:02:48,680 --> 00:02:51,510
So it just kind of rolls through. And you can adjust that with

47
00:02:51,510 --> 00:02:55,660
that ThrottleLimit parameter. And I should be able to see the

48
00:02:55,660 --> 00:02:57,860
same number of results.

49
00:02:57,860 --> 00:02:59,540
So, I got the same results,

50
00:02:59,540 --> 00:03:04,200
except it took half the time. So imagine the performance gains

51
00:03:04,200 --> 00:03:08,410
when you scale out in this example. If I had 50 servers, and

52
00:03:08,410 --> 00:03:10,810
let's say I was retrieving 10,000 logs,

53
00:03:10,810 --> 00:03:15,030
you know the ‑parallel is going to make a big difference. However,

54
00:03:15,030 --> 00:03:19,220
if you're not going to need the results right away and it takes longer,

55
00:03:19,220 --> 00:03:22,370
but you're not going to get to the results until after it's done anyway,

56
00:03:22,370 --> 00:03:23,940
yeah maybe it doesn't matter.

57
00:03:23,940 --> 00:03:28,480
So, the parallel is there. It's something that you can play with. This

58
00:03:28,480 --> 00:03:32,170
is why I think PowerShell is a fantastic management tool once you

59
00:03:32,170 --> 00:03:36,330
understand the basics. The fundamental techniques and concepts apply to

60
00:03:36,330 --> 00:03:41,140
everything. Once you learn the basics, you can apply them everywhere.

61
00:03:41,140 --> 00:03:42,810
Is the boss dropping Azure on you?

62
00:03:42,810 --> 00:03:43,800
No problem.

63
00:03:43,800 --> 00:03:47,240
Discover the Azure cmdlets and parameters and then use them the same

64
00:03:47,240 --> 00:03:50,640
way you would like Get‑Servers and Get‑Process.

65
00:03:50,640 --> 00:03:50,930
Now,

66
00:03:50,930 --> 00:03:59,000
I still need to show you one more important skill when working with the PowerShell pipeline, and I'll cover that next. So, hurry back.

