1
00:00:00,690 --> 00:00:01,930
So let's go and have a look at how we

2
00:00:01,930 --> 00:00:05,740
create remote background jobs. Okay, so

3
00:00:05,740 --> 00:00:08,780
we're back in our PowerShell console. Now,

4
00:00:08,780 --> 00:00:09,960
of course, one of the things that we can

5
00:00:09,960 --> 00:00:12,360
do to actually identify commands that we

6
00:00:12,360 --> 00:00:15,520
can use for remoting is to use the Get

7
00:00:15,520 --> 00:00:19,300
command. We can then use the Where option

8
00:00:19,300 --> 00:00:21,420
and then, of course, using our squirrelly

9
00:00:21,420 --> 00:00:26,530
brackets as always, we can use this $, and

10
00:00:26,530 --> 00:00:27,910
I'm going to use parameters, which is a

11
00:00:27,910 --> 00:00:31,970
value, and then keys, and then I can say

12
00:00:31,970 --> 00:00:37,710
‑contains ComputerName. So what this will

13
00:00:37,710 --> 00:00:41,930
do is, this will show me any commands that

14
00:00:41,930 --> 00:00:45,060
take a property of ComputerName. So you

15
00:00:45,060 --> 00:00:46,020
can see we've got this, like,

16
00:00:46,020 --> 00:00:49,180
New‑PSSession, Invoke‑Command, Remove,

17
00:00:49,180 --> 00:00:52,360
Restart‑Computer, etc., Connect‑PSSession.

18
00:00:52,360 --> 00:00:54,030
Now, what we're not going to do is really

19
00:00:54,030 --> 00:00:57,370
talk about just connecting directly to a

20
00:00:57,370 --> 00:00:59,710
remote machine and just running something.

21
00:00:59,710 --> 00:01:00,910
For example, I'm not going to talk about

22
00:01:00,910 --> 00:01:02,870
Restart‑Computer or Stop. This is really

23
00:01:02,870 --> 00:01:06,130
focused on being able to get a specific

24
00:01:06,130 --> 00:01:09,260
session to actually run some specific

25
00:01:09,260 --> 00:01:13,050
things. So the way that we utilize this

26
00:01:13,050 --> 00:01:15,490
and execute a remote background job is

27
00:01:15,490 --> 00:01:18,220
really focused on using, for example,

28
00:01:18,220 --> 00:01:21,020
either interactive or non‑interactive

29
00:01:21,020 --> 00:01:23,790
PowerShell sessions. Now this is achieved

30
00:01:23,790 --> 00:01:26,980
by either using, entering into a

31
00:01:26,980 --> 00:01:31,080
new‑PSSession or invoking a command to do

32
00:01:31,080 --> 00:01:33,010
the non‑interactive. Now you understand

33
00:01:33,010 --> 00:01:33,930
the difference here between a

34
00:01:33,930 --> 00:01:35,880
non‑interactive and interactive.

35
00:01:35,880 --> 00:01:39,370
Interactive means that my session here

36
00:01:39,370 --> 00:01:42,300
gets overtaken by the remote session on

37
00:01:42,300 --> 00:01:45,590
the machine, and then my non‑interactive

38
00:01:45,590 --> 00:01:47,390
means that I don't have a command prompt

39
00:01:47,390 --> 00:01:50,450
whatsoever. Now one of the commands that

40
00:01:50,450 --> 00:01:53,170
is available to us here is what's called

41
00:01:53,170 --> 00:01:57,650
Enter‑PSSession. This will then take a

42
00:01:57,650 --> 00:02:00,360
‑ComputerName, which I'm going to use my

43
00:02:00,360 --> 00:02:01,920
IP address of the machine that I'm

44
00:02:01,920 --> 00:02:04,160
connecting to because I don't have a name

45
00:02:04,160 --> 00:02:06,410
resolution for it. And then, of course,

46
00:02:06,410 --> 00:02:08,080
one thing that we want to do is pass a

47
00:02:08,080 --> 00:02:10,650
credential. And this is my TRAINER

48
00:02:10,650 --> 00:02:15,940
machine, and it's my liamcleary account.

49
00:02:15,940 --> 00:02:17,650
Now notice what happens. It'll say okay,

50
00:02:17,650 --> 00:02:19,250
prompt me for the credentials. Now, of

51
00:02:19,250 --> 00:02:22,050
course, if my machine was on the same

52
00:02:22,050 --> 00:02:23,920
domain as the other machine, then I

53
00:02:23,920 --> 00:02:25,450
wouldn't get prompted if I was logged in

54
00:02:25,450 --> 00:02:27,760
with the same account. So let me just log

55
00:02:27,760 --> 00:02:33,100
in like so. And then of course you'll see

56
00:02:33,100 --> 00:02:35,920
it entered the remote session, and I'm now

57
00:02:35,920 --> 00:02:37,890
what it looks like is connected to that

58
00:02:37,890 --> 00:02:41,010
machine. We can test this by simply saying

59
00:02:41,010 --> 00:02:42,910
hostname, and you'll see comes back as

60
00:02:42,910 --> 00:02:45,690
TRAINER, which clearly isn't my machine.

61
00:02:45,690 --> 00:02:48,370
If I exit that session and then type

62
00:02:48,370 --> 00:02:49,790
hostname again, you can see I'm actually

63
00:02:49,790 --> 00:02:53,000
on OFFICE. So entering the PSSession will

64
00:02:53,000 --> 00:02:56,230
take me into the existing session. Now of

65
00:02:56,230 --> 00:02:58,250
course, what we can do is‑‑‑that was an

66
00:02:58,250 --> 00:03:00,780
interactive PowerShell session. Now what

67
00:03:00,780 --> 00:03:03,440
about if we wanted to create a

68
00:03:03,440 --> 00:03:05,960
non‑interactive PowerShell? Well, that

69
00:03:05,960 --> 00:03:08,700
one's done using the same process that we

70
00:03:08,700 --> 00:03:11,430
did previously, where we're using the

71
00:03:11,430 --> 00:03:15,210
Invoke‑Command, and we can invoke a

72
00:03:15,210 --> 00:03:17,590
session. Now, in order for us to do that,

73
00:03:17,590 --> 00:03:19,010
we're not actually going to invoke it

74
00:03:19,010 --> 00:03:21,370
first; we're going to actually say I want

75
00:03:21,370 --> 00:03:23,640
to create a new session. Now notice the

76
00:03:23,640 --> 00:03:26,030
difference here. We used Enter‑PSSession

77
00:03:26,030 --> 00:03:29,090
to get an interactive one. New‑PSSession,

78
00:03:29,090 --> 00:03:31,120
which you would think is going to create

79
00:03:31,120 --> 00:03:33,250
an interactive one, doesn't. It creates

80
00:03:33,250 --> 00:03:35,790
the non‑interactive one. So I'm going to

81
00:03:35,790 --> 00:03:38,590
say ‑ComputerName, and I'll type my IP

82
00:03:38,590 --> 00:03:40,320
address again to the machine that I'm

83
00:03:40,320 --> 00:03:42,120
using, and I'm going to pass my

84
00:03:42,120 --> 00:03:44,360
credentials because I'm obviously not on

85
00:03:44,360 --> 00:03:52,440
that machine. And I'll put my password in.

86
00:03:52,440 --> 00:03:54,980
Okay, so I now have a session. But notice

87
00:03:54,980 --> 00:03:56,950
what happened this time. I didn't get an

88
00:03:56,950 --> 00:04:00,060
interactive session. But if I choose

89
00:04:00,060 --> 00:04:04,210
Get‑PSSession, you'll see it scrolls right

90
00:04:04,210 --> 00:04:06,170
to the bottom, and you can see I have a

91
00:04:06,170 --> 00:04:09,720
remote machine connected with an ID of 4.

92
00:04:09,720 --> 00:04:11,910
So that allows me to create a

93
00:04:11,910 --> 00:04:15,410
non‑interactive session. Now, once we have

94
00:04:15,410 --> 00:04:17,230
these types of sessions, I'm just going to

95
00:04:17,230 --> 00:04:20,440
clear that out, obviously we're trying to

96
00:04:20,440 --> 00:04:22,930
execute certain types of commands. So what

97
00:04:22,930 --> 00:04:24,770
I'm going to do first is, I'm going to get

98
00:04:24,770 --> 00:04:31,300
that PSSession again. There we go,

99
00:04:31,300 --> 00:04:34,030
PSSession. It's number 4. I'm going to say

100
00:04:34,030 --> 00:04:41,240
Remove‑PSSession ‑Id, and 4. Okay, so what

101
00:04:41,240 --> 00:04:43,570
we'll do is, we'll look a just using the

102
00:04:43,570 --> 00:04:46,620
Invoke‑Command to execute something on the

103
00:04:46,620 --> 00:04:49,560
other device. So two different types of

104
00:04:49,560 --> 00:04:51,850
remote background jobs. One is to just

105
00:04:51,850 --> 00:04:55,040
execute a job on the other machine. The

106
00:04:55,040 --> 00:04:58,780
other one is to create a non‑interactive

107
00:04:58,780 --> 00:05:00,910
way of doing it. So I'm actually going to

108
00:05:00,910 --> 00:05:04,910
say Invoke‑Command. Now notice

109
00:05:04,910 --> 00:05:08,300
Invoke‑Command has a ComputerName also, so

110
00:05:08,300 --> 00:05:12,440
I'm going to type my IP address. I'm going

111
00:05:12,440 --> 00:05:19,520
to pass my credential, TRAINER, and then

112
00:05:19,520 --> 00:05:23,660
my username and then my ScriptBlock, which

113
00:05:23,660 --> 00:05:26,070
we can always pass, remember. I'm going to

114
00:05:26,070 --> 00:05:28,420
choose Get‑Process and I'm going to enter

115
00:05:28,420 --> 00:05:30,110
that. Now of course it's going to prompt

116
00:05:30,110 --> 00:05:34,330
me for credentials and this will now go

117
00:05:34,330 --> 00:05:37,210
ahead, connect to that with a machine, and

118
00:05:37,210 --> 00:05:39,810
then return the results instantly back.

119
00:05:39,810 --> 00:05:41,590
Now, of course, it's going to stream them

120
00:05:41,590 --> 00:05:43,640
from the other machine so it can take some

121
00:05:43,640 --> 00:05:45,650
time to execute back. But you'll see it's

122
00:05:45,650 --> 00:05:47,260
kind of slowly bringing them back. I've

123
00:05:47,260 --> 00:05:48,890
got Skype running and a whole bunch of

124
00:05:48,890 --> 00:05:51,210
other bits and pieces. But when we ran

125
00:05:51,210 --> 00:05:53,620
that command, that created a background

126
00:05:53,620 --> 00:05:57,510
job that was on that specific machine. So

127
00:05:57,510 --> 00:05:59,050
what does that look like? Well, let's

128
00:05:59,050 --> 00:06:01,630
remember how we retrieve jobs. So I'm

129
00:06:01,630 --> 00:06:03,900
going to say Get‑Job. Now notice there was

130
00:06:03,900 --> 00:06:07,740
no jobs, but actually, I just created a

131
00:06:07,740 --> 00:06:11,340
job. So where did that job come from?

132
00:06:11,340 --> 00:06:12,730
Well, you have to remember that what's

133
00:06:12,730 --> 00:06:14,070
happening is, when we pass the

134
00:06:14,070 --> 00:06:17,100
Invoke‑Command to the other computer, the

135
00:06:17,100 --> 00:06:21,150
job is being executed over there, not on

136
00:06:21,150 --> 00:06:23,520
this machine over here. Now, if we go to

137
00:06:23,520 --> 00:06:25,780
the the actual commands, you can see when

138
00:06:25,780 --> 00:06:27,890
we loop through them that there's no

139
00:06:27,890 --> 00:06:30,510
option to say I want to go and get the

140
00:06:30,510 --> 00:06:33,200
job, whatever it would be. I can't see

141
00:06:33,200 --> 00:06:37,800
that. So what we can do is go back, and

142
00:06:37,800 --> 00:06:41,000
I'm going to say IncludeChildJob, and I'm

143
00:06:41,000 --> 00:06:45,220
going to execute this one. Now what am I

144
00:06:45,220 --> 00:06:48,240
doing here? I'm invoking another job to

145
00:06:48,240 --> 00:06:50,430
get to the other machine, telling it to

146
00:06:50,430 --> 00:06:53,590
get the list of jobs and child jobs for

147
00:06:53,590 --> 00:06:55,180
that specific one. Now notice what

148
00:06:55,180 --> 00:06:56,430
happened. It comes back and says there's

149
00:06:56,430 --> 00:06:59,170
nothing, and that's because the jobs have

150
00:06:59,170 --> 00:07:01,220
completed and there's no jobs to be able

151
00:07:01,220 --> 00:07:04,120
to come back. So just be aware that when

152
00:07:04,120 --> 00:07:06,230
you have jobs backwards and forwards, it's

153
00:07:06,230 --> 00:07:08,740
often quite hard to remember where they

154
00:07:08,740 --> 00:07:10,680
are. You think they may be local, but

155
00:07:10,680 --> 00:07:13,800
actually they're more external. Now, the

156
00:07:13,800 --> 00:07:15,980
other option is to create remote

157
00:07:15,980 --> 00:07:18,610
background jobs on either single or

158
00:07:18,610 --> 00:07:21,060
multiple computers. So what we can do this

159
00:07:21,060 --> 00:07:23,850
time is, we can use the same principle

160
00:07:23,850 --> 00:07:31,580
where we say New‑PSSession ‑ComputerName.

161
00:07:31,580 --> 00:07:36,220
I'm going to type my IP address, and I'm

162
00:07:36,220 --> 00:07:37,950
going to pass my credential because I know

163
00:07:37,950 --> 00:07:44,740
I'm going to get asked for that, okay, and

164
00:07:44,740 --> 00:07:50,160
password. Okay, now if I just do session,

165
00:07:50,160 --> 00:07:51,660
you can see it gives me my session that's

166
00:07:51,660 --> 00:07:56,500
opened. What I can then do is actually run

167
00:07:56,500 --> 00:08:01,850
Invoke‑Command. I'm now going to inject

168
00:08:01,850 --> 00:08:06,050
into the current session my ScriptBlock,

169
00:08:06,050 --> 00:08:10,220
which is the same as before, Process, and

170
00:08:10,220 --> 00:08:12,690
then notice what I'm going to do now. I'm

171
00:08:12,690 --> 00:08:16,110
going to pass that as an AsJob statement.

172
00:08:16,110 --> 00:08:17,910
Now notice what happened the last time. We

173
00:08:17,910 --> 00:08:20,990
did Invoke‑Command, we didn't use AsJob.

174
00:08:20,990 --> 00:08:22,940
We tried to get the job and there was no

175
00:08:22,940 --> 00:08:25,170
job. And that's because we didn't pass in

176
00:08:25,170 --> 00:08:28,100
the AsJob. Now there we have the jobs that

177
00:08:28,100 --> 00:08:31,040
have come back. Now, let's just do Get‑Job

178
00:08:31,040 --> 00:08:33,940
from here, okay. You can see that it's now

179
00:08:33,940 --> 00:08:36,400
listing me the jobs that have been

180
00:08:36,400 --> 00:08:39,420
initiated, and so I could now manage those

181
00:08:39,420 --> 00:08:42,600
as I would any other job. So to initiate a

182
00:08:42,600 --> 00:08:45,810
remote background job, it's as simple as

183
00:08:45,810 --> 00:08:48,570
either invoking the command directly to

184
00:08:48,570 --> 00:08:50,890
the machine that you want to put the job

185
00:08:50,890 --> 00:08:54,280
on or simply passing in a New‑PSSession

186
00:08:54,280 --> 00:08:56,680
object with a series of computers or

187
00:08:56,680 --> 00:08:59,030
single ones and then invoking the command

188
00:08:59,030 --> 00:09:02,570
as a Start‑Job. Now we also have the other

189
00:09:02,570 --> 00:09:05,200
approach that we can utilize, which, if we

190
00:09:05,200 --> 00:09:07,180
were to go back to our command here and

191
00:09:07,180 --> 00:09:10,060
not use that, we could actually come in

192
00:09:10,060 --> 00:09:17,950
and say, Start‑Job. Let me get rid of all

193
00:09:17,950 --> 00:09:24,130
of my Get‑Process. And let me do name

194
00:09:24,130 --> 00:09:28,050
ScriptBlock. And notice how we can start

195
00:09:28,050 --> 00:09:31,340
to nest pieces in here, so I'm going to

196
00:09:31,340 --> 00:09:34,310
say Get‑Process. So what we've now done is

197
00:09:34,310 --> 00:09:38,170
initiated a new invoke‑Command. The

198
00:09:38,170 --> 00:09:40,700
ScriptBlock for that is to start a job,

199
00:09:40,700 --> 00:09:42,560
and then the Start‑Job is going to kick of

200
00:09:42,560 --> 00:09:46,990
to Get‑Process. So now if I put Get‑Job,

201
00:09:46,990 --> 00:09:50,290
notice locally I only get Job3, so where

202
00:09:50,290 --> 00:09:54,660
is Job1? Well, Job1 exists on that other

203
00:09:54,660 --> 00:09:57,400
machine. So just be aware that it can be

204
00:09:57,400 --> 00:09:59,090
quite confusing when you're creating the

205
00:09:59,090 --> 00:10:03,000
remote background jobs of where those jobs actually get created.

