1
00:00:00,340 --> 00:00:02,310
So let's talk about the PowerShell job

2
00:00:02,310 --> 00:00:05,190
object model. So let's first talk about

3
00:00:05,190 --> 00:00:08,140
the syntax itself. Well, it's broken down

4
00:00:08,140 --> 00:00:10,880
into specific pieces, so the first thing

5
00:00:10,880 --> 00:00:13,940
is the command that we wish to execute. So

6
00:00:13,940 --> 00:00:15,680
let's say, for example, we're going to use

7
00:00:15,680 --> 00:00:18,400
Start‑Job, so that will be the command

8
00:00:18,400 --> 00:00:21,630
that we execute. We then have the name

9
00:00:21,630 --> 00:00:24,470
that we wish to associate to this command.

10
00:00:24,470 --> 00:00:26,780
So if I'm going to start a job, I can give

11
00:00:26,780 --> 00:00:29,500
the job an actual name that I can use to

12
00:00:29,500 --> 00:00:31,850
retrieve it back later. Now we don't have

13
00:00:31,850 --> 00:00:34,340
to pass that. It'll simply give it a

14
00:00:34,340 --> 00:00:37,790
random name. Then we can pass a script

15
00:00:37,790 --> 00:00:40,950
block, if required, so what other commands

16
00:00:40,950 --> 00:00:43,240
are we going to execute as part of this?

17
00:00:43,240 --> 00:00:45,660
And then an optional one here is to pass

18
00:00:45,660 --> 00:00:48,170
credential and authentication information,

19
00:00:48,170 --> 00:00:51,270
so we may need to pass domain/user. For

20
00:00:51,270 --> 00:00:53,720
example, if we're using an Invoke‑Command

21
00:00:53,720 --> 00:00:56,160
request to remotely connect to something,

22
00:00:56,160 --> 00:00:57,600
we'll want to pass a credential and

23
00:00:57,600 --> 00:01:00,360
authentication. We can then pass an

24
00:01:00,360 --> 00:01:03,540
arguments list of various parameters that

25
00:01:03,540 --> 00:01:06,120
might be needed, and then last one is the

26
00:01:06,120 --> 00:01:08,250
optional one too, where we can pass the

27
00:01:08,250 --> 00:01:10,950
PowerShell version that we wish to use to

28
00:01:10,950 --> 00:01:13,960
execute this PowerShell job. So every

29
00:01:13,960 --> 00:01:16,440
single PowerShell job command will be

30
00:01:16,440 --> 00:01:18,590
based around this same syntax, the

31
00:01:18,590 --> 00:01:22,300
command, the name, the execution block,

32
00:01:22,300 --> 00:01:25,290
any credentials, any arguments, and then

33
00:01:25,290 --> 00:01:28,560
if there's a version that's required. So

34
00:01:28,560 --> 00:01:29,940
if we look at the syntax here for

35
00:01:29,940 --> 00:01:32,500
Start‑Job, you can see that we have a

36
00:01:32,500 --> 00:01:35,140
series of parameters available to us.

37
00:01:35,140 --> 00:01:37,110
We've got the name, which we talked about

38
00:01:37,110 --> 00:01:39,100
as an optional; the ScriptBlock, which is

39
00:01:39,100 --> 00:01:41,520
the execution commands; we've then got

40
00:01:41,520 --> 00:01:43,850
credential, which will be the username,

41
00:01:43,850 --> 00:01:46,430
password, etc.; authentication, we can

42
00:01:46,430 --> 00:01:49,040
specify a mechanism for authentication.

43
00:01:49,040 --> 00:01:51,550
We've then got InitializationScript, so

44
00:01:51,550 --> 00:01:53,730
something that we're going to execute

45
00:01:53,730 --> 00:01:55,840
straightaway. We can then set the

46
00:01:55,840 --> 00:01:57,910
WorkingDirectory. This is important

47
00:01:57,910 --> 00:01:59,910
because if we're referencing, for example,

48
00:01:59,910 --> 00:02:03,150
another ps1 file that we've created, we

49
00:02:03,150 --> 00:02:05,030
may not want to pass the full path. We

50
00:02:05,030 --> 00:02:07,310
could just say, start this job in this

51
00:02:07,310 --> 00:02:09,310
working directory and then from that

52
00:02:09,310 --> 00:02:11,790
point, use everything that's stored there.

53
00:02:11,790 --> 00:02:14,810
We can run it as a 32‑bit process, we can

54
00:02:14,810 --> 00:02:17,170
obviously pass in the PowerShell version,

55
00:02:17,170 --> 00:02:18,950
and then we have InputObjects and

56
00:02:18,950 --> 00:02:22,570
ArgumentLists if they are required. So

57
00:02:22,570 --> 00:02:24,700
some examples of this, if we wanted to

58
00:02:24,700 --> 00:02:27,440
start a standard background job, I'm going

59
00:02:27,440 --> 00:02:30,770
to utilize Start‑Job with a ScriptBlock.

60
00:02:30,770 --> 00:02:32,300
And in this example, I'm going to just

61
00:02:32,300 --> 00:02:35,500
call Get‑Process, which is another regular

62
00:02:35,500 --> 00:02:37,550
PowerShell cmdlet, and I'm going to go and

63
00:02:37,550 --> 00:02:40,110
say get me the PowerShell window. And all

64
00:02:40,110 --> 00:02:42,550
this will do when it executes is return

65
00:02:42,550 --> 00:02:45,610
back the fact that it found the process. I

66
00:02:45,610 --> 00:02:47,480
could also do a similar thing, but this

67
00:02:47,480 --> 00:02:50,070
time I'm going to pass in a script

68
00:02:50,070 --> 00:02:53,380
instead. So I can say Start‑Job, give it a

69
00:02:53,380 --> 00:02:56,720
FilePath and say, create this job, but I

70
00:02:56,720 --> 00:02:59,910
want you to execute whatever's inside this

71
00:02:59,910 --> 00:03:03,340
PowerShell 1 script. If I need to pass

72
00:03:03,340 --> 00:03:05,790
arguments to this, so for example, I could

73
00:03:05,790 --> 00:03:08,460
say Start‑Job ‑ScriptBlock again, which is

74
00:03:08,460 --> 00:03:11,870
a kind of common code here. Then in my

75
00:03:11,870 --> 00:03:14,960
Get‑Process, I'm going to say ‑Name and

76
00:03:14,960 --> 00:03:16,760
then I've got this new variable called

77
00:03:16,760 --> 00:03:20,460
args, and args will be the arguments list

78
00:03:20,460 --> 00:03:22,660
of things that I want to pass. And then we

79
00:03:22,660 --> 00:03:26,380
can populate the ArgumentList property, or

80
00:03:26,380 --> 00:03:28,790
parameter, with the values. So in this

81
00:03:28,790 --> 00:03:29,940
instance, it's going to look for

82
00:03:29,940 --> 00:03:33,250
PowerShell, the pwsh version of

83
00:03:33,250 --> 00:03:35,630
PowerShell, and then notepad processes

84
00:03:35,630 --> 00:03:39,250
that are executing. Our last example here

85
00:03:39,250 --> 00:03:41,570
is to actually set the WorkingDirectory.

86
00:03:41,570 --> 00:03:43,590
And as I talked about previously, this is

87
00:03:43,590 --> 00:03:45,440
about defining the directory where you

88
00:03:45,440 --> 00:03:47,900
wish this to run, and then it means I can

89
00:03:47,900 --> 00:03:50,080
map that to something like a FilePath. So

90
00:03:50,080 --> 00:03:52,000
I'm going to say, start a job in my

91
00:03:52,000 --> 00:03:54,440
C:\Scripts folder and then execute

92
00:03:54,440 --> 00:03:57,240
Script.ps1. Instead of me having to put

93
00:03:57,240 --> 00:03:59,700
the full path in the filePath, I can just

94
00:03:59,700 --> 00:04:05,000
make sure that the WorkingDirectory has been set to where those files are.

