1
00:00:00,640 --> 00:00:03,990
So what are PowerShell jobs? Well a

2
00:00:03,990 --> 00:00:06,930
PowerShell job is a background job that

3
00:00:06,930 --> 00:00:10,260
runs a command without interacting with

4
00:00:10,260 --> 00:00:12,810
that specific session. While that's

5
00:00:12,810 --> 00:00:14,940
running, you can continue to work in the

6
00:00:14,940 --> 00:00:17,590
same PowerShell session without

7
00:00:17,590 --> 00:00:20,330
interruption while the job runs in the

8
00:00:20,330 --> 00:00:24,460
background. There were two different types

9
00:00:24,460 --> 00:00:26,610
of PowerShell jobs that can be ran. The

10
00:00:26,610 --> 00:00:28,380
first is the default, which is a

11
00:00:28,380 --> 00:00:31,320
PowerShell 7 version of that. We simply

12
00:00:31,320 --> 00:00:34,010
just say start job, and it initiates that

13
00:00:34,010 --> 00:00:36,740
job. We can also run a backwards

14
00:00:36,740 --> 00:00:39,100
compatibility version of that, which is

15
00:00:39,100 --> 00:00:42,870
PowerShell 5.1. Now to execute these types

16
00:00:42,870 --> 00:00:44,950
of jobs, let's say that we first wanted to

17
00:00:44,950 --> 00:00:46,930
retrieve the version of the PowerShell

18
00:00:46,930 --> 00:00:49,250
that we have. So to do this, we'll use

19
00:00:49,250 --> 00:00:52,310
PSVersionTable and then get the PSVersion

20
00:00:52,310 --> 00:00:56,160
property from the response. If I wanted to

21
00:00:56,160 --> 00:00:59,840
execute this using PowerShell version 5.1,

22
00:00:59,840 --> 00:01:03,400
I would call Start‑Job. ScriptBlock is the

23
00:01:03,400 --> 00:01:06,200
property or at least the parameter that we

24
00:01:06,200 --> 00:01:09,700
use to pass in specific syntax we wish to

25
00:01:09,700 --> 00:01:11,370
execute. So we're going to call the

26
00:01:11,370 --> 00:01:13,530
PSVersion. And then there's a property

27
00:01:13,530 --> 00:01:16,960
called PSVersion, and I can pass in 5.1,

28
00:01:16,960 --> 00:01:21,770
7.0, etc. So we can specifically tell this

29
00:01:21,770 --> 00:01:26,230
command to run using 5.1. Then, of course,

30
00:01:26,230 --> 00:01:28,120
if I want to execute in the native

31
00:01:28,120 --> 00:01:30,760
PowerShell 7, I don't need to pass the

32
00:01:30,760 --> 00:01:34,850
PSVersion. I can say Start‑Job ScriptBlock

33
00:01:34,850 --> 00:01:36,620
and then pass in the code that we wish to

34
00:01:36,620 --> 00:01:39,500
execute. So this just provides us a way of

35
00:01:39,500 --> 00:01:41,770
backwards compatibility. For example, you

36
00:01:41,770 --> 00:01:44,060
may have scripts that don't support some

37
00:01:44,060 --> 00:01:46,300
of the features in PowerShell 7. or you

38
00:01:46,300 --> 00:01:48,800
may have something that uses a method that

39
00:01:48,800 --> 00:01:51,410
doesn't exist in PowerShell 7 anymore. So

40
00:01:51,410 --> 00:01:53,660
we can at least backwards compat that

41
00:01:53,660 --> 00:01:57,140
PowerShell script that we execute. There

42
00:01:57,140 --> 00:01:59,710
are two core PowerShell job types. The

43
00:01:59,710 --> 00:02:02,140
first one is a background job. This is

44
00:02:02,140 --> 00:02:04,400
only available within the current

45
00:02:04,400 --> 00:02:06,720
PowerShell session. So what I mean by that

46
00:02:06,720 --> 00:02:09,200
is when you launch the PowerShell window,

47
00:02:09,200 --> 00:02:12,030
initiate Start‑Job, you can retrieve

48
00:02:12,030 --> 00:02:14,580
information about that job within that

49
00:02:14,580 --> 00:02:17,460
same window. If I was to launch a new

50
00:02:17,460 --> 00:02:20,650
PowerShell window, I have no access to

51
00:02:20,650 --> 00:02:23,030
that existing background job because it

52
00:02:23,030 --> 00:02:25,830
doesn't exist in the same session. The

53
00:02:25,830 --> 00:02:27,110
second option that we're not going to

54
00:02:27,110 --> 00:02:30,210
speak about at all during this course is

55
00:02:30,210 --> 00:02:32,560
scheduled jobs. This was a process that we

56
00:02:32,560 --> 00:02:34,930
could utilize in previous versions of

57
00:02:34,930 --> 00:02:37,800
PowerShell where this creates a scheduled

58
00:02:37,800 --> 00:02:40,380
background job that's independent of the

59
00:02:40,380 --> 00:02:42,950
current PowerShell session. So that means

60
00:02:42,950 --> 00:02:44,950
the same example is before, I kick off

61
00:02:44,950 --> 00:02:47,240
something in one PowerShell window, I can

62
00:02:47,240 --> 00:02:49,360
open the second PowerShell window, and I

63
00:02:49,360 --> 00:02:53,450
can communicate between the two. Now if we

64
00:02:53,450 --> 00:02:55,520
look at the job types, they're broken down

65
00:02:55,520 --> 00:02:58,060
even further. So the first one we've

66
00:02:58,060 --> 00:03:00,230
talked about is the background job. This

67
00:03:00,230 --> 00:03:02,930
is how you initiate it by using Start‑Job

68
00:03:02,930 --> 00:03:05,340
cmdlet, and that will create whatever

69
00:03:05,340 --> 00:03:08,040
script block or task that you've created

70
00:03:08,040 --> 00:03:11,150
and push it to a background job. The other

71
00:03:11,150 --> 00:03:12,780
one is what's referred to was a remote

72
00:03:12,780 --> 00:03:15,280
job. So this means I can use what's called

73
00:03:15,280 --> 00:03:18,740
Invoke‑Command, which says invoke this

74
00:03:18,740 --> 00:03:20,900
specific thing, whether it's another

75
00:03:20,900 --> 00:03:23,110
PowerShell script or a command. And then I

76
00:03:23,110 --> 00:03:27,110
use a property called AsJob. When I use

77
00:03:27,110 --> 00:03:30,180
‑AsJob, it pushes that to become a

78
00:03:30,180 --> 00:03:32,560
background job. So you'll utilize this,

79
00:03:32,560 --> 00:03:34,780
for example, if we're trying to execute

80
00:03:34,780 --> 00:03:37,030
commands on remote servers or

81
00:03:37,030 --> 00:03:39,190
workstations, and you wanted to run it as

82
00:03:39,190 --> 00:03:42,520
a job. We then have what's called a

83
00:03:42,520 --> 00:03:45,900
PSWorkflowJob. So this is started again by

84
00:03:45,900 --> 00:03:49,520
using the AsJob parameter of a specific

85
00:03:49,520 --> 00:03:52,890
PowerShell workflow job. We also then have

86
00:03:52,890 --> 00:03:56,290
CIMJobs and WMIJobs, which also can take

87
00:03:56,290 --> 00:03:59,030
the AsJob parameter, and that gets bundled

88
00:03:59,030 --> 00:04:02,580
into the CD XML or the WMI modules that

89
00:04:02,580 --> 00:04:04,960
might be utilized. We then also have

90
00:04:04,960 --> 00:04:07,920
PSEventJobs, and this is where you can use

91
00:04:07,920 --> 00:04:11,380
Register‑ObjectEvent, and then you add an

92
00:04:11,380 --> 00:04:13,920
action parameter. An event job, for

93
00:04:13,920 --> 00:04:15,460
example, can be used when you kick

94
00:04:15,460 --> 00:04:17,890
something off. I want an event to happen,

95
00:04:17,890 --> 00:04:20,420
and then I want this to happen too. So

96
00:04:20,420 --> 00:04:23,000
those are the different types of PowerShell jobs that we have.

