So let's go and have a look at how we
create remote background jobs. Okay, so
we're back in our PowerShell console. Now,
of course, one of the things that we can
do to actually identify commands that we
can use for remoting is to use the Get
command. We can then use the Where option
and then, of course, using our squirrelly
brackets as always, we can use this $, and
I'm going to use parameters, which is a
value, and then keys, and then I can say
‑contains ComputerName. So what this will
do is, this will show me any commands that
take a property of ComputerName. So you
can see we've got this, like,
New‑PSSession, Invoke‑Command, Remove,
Restart‑Computer, etc., Connect‑PSSession.
Now, what we're not going to do is really
talk about just connecting directly to a
remote machine and just running something.
For example, I'm not going to talk about
Restart‑Computer or Stop. This is really
focused on being able to get a specific
session to actually run some specific
things. So the way that we utilize this
and execute a remote background job is
really focused on using, for example,
either interactive or non‑interactive
PowerShell sessions. Now this is achieved
by either using, entering into a
new‑PSSession or invoking a command to do
the non‑interactive. Now you understand
the difference here between a
non‑interactive and interactive.
Interactive means that my session here
gets overtaken by the remote session on
the machine, and then my non‑interactive
means that I don't have a command prompt
whatsoever. Now one of the commands that
is available to us here is what's called
Enter‑PSSession. This will then take a
‑ComputerName, which I'm going to use my
IP address of the machine that I'm
connecting to because I don't have a name
resolution for it. And then, of course,
one thing that we want to do is pass a
credential. And this is my TRAINER
machine, and it's my liamcleary account.
Now notice what happens. It'll say okay,
prompt me for the credentials. Now, of
course, if my machine was on the same
domain as the other machine, then I
wouldn't get prompted if I was logged in
with the same account. So let me just log
in like so. And then of course you'll see
it entered the remote session, and I'm now
what it looks like is connected to that
machine. We can test this by simply saying
hostname, and you'll see comes back as
TRAINER, which clearly isn't my machine.
If I exit that session and then type
hostname again, you can see I'm actually
on OFFICE. So entering the PSSession will
take me into the existing session. Now of
course, what we can do is‑‑‑that was an
interactive PowerShell session. Now what
about if we wanted to create a
non‑interactive PowerShell? Well, that
one's done using the same process that we
did previously, where we're using the
Invoke‑Command, and we can invoke a
session. Now, in order for us to do that,
we're not actually going to invoke it
first; we're going to actually say I want
to create a new session. Now notice the
difference here. We used Enter‑PSSession
to get an interactive one. New‑PSSession,
which you would think is going to create
an interactive one, doesn't. It creates
the non‑interactive one. So I'm going to
say ‑ComputerName, and I'll type my IP
address again to the machine that I'm
using, and I'm going to pass my
credentials because I'm obviously not on
that machine. And I'll put my password in.
Okay, so I now have a session. But notice
what happened this time. I didn't get an
interactive session. But if I choose
Get‑PSSession, you'll see it scrolls right
to the bottom, and you can see I have a
remote machine connected with an ID of 4.
So that allows me to create a
non‑interactive session. Now, once we have
these types of sessions, I'm just going to
clear that out, obviously we're trying to
execute certain types of commands. So what
I'm going to do first is, I'm going to get
that PSSession again. There we go,
PSSession. It's number 4. I'm going to say
Remove‑PSSession ‑Id, and 4. Okay, so what
we'll do is, we'll look a just using the
Invoke‑Command to execute something on the
other device. So two different types of
remote background jobs. One is to just
execute a job on the other machine. The
other one is to create a non‑interactive
way of doing it. So I'm actually going to
say Invoke‑Command. Now notice
Invoke‑Command has a ComputerName also, so
I'm going to type my IP address. I'm going
to pass my credential, TRAINER, and then
my username and then my ScriptBlock, which
we can always pass, remember. I'm going to
choose Get‑Process and I'm going to enter
that. Now of course it's going to prompt
me for credentials and this will now go
ahead, connect to that with a machine, and
then return the results instantly back.
Now, of course, it's going to stream them
from the other machine so it can take some
time to execute back. But you'll see it's
kind of slowly bringing them back. I've
got Skype running and a whole bunch of
other bits and pieces. But when we ran
that command, that created a background
job that was on that specific machine. So
what does that look like? Well, let's
remember how we retrieve jobs. So I'm
going to say Get‑Job. Now notice there was
no jobs, but actually, I just created a
job. So where did that job come from?
Well, you have to remember that what's
happening is, when we pass the
Invoke‑Command to the other computer, the
job is being executed over there, not on
this machine over here. Now, if we go to
the the actual commands, you can see when
we loop through them that there's no
option to say I want to go and get the
job, whatever it would be. I can't see
that. So what we can do is go back, and
I'm going to say IncludeChildJob, and I'm
going to execute this one. Now what am I
doing here? I'm invoking another job to
get to the other machine, telling it to
get the list of jobs and child jobs for
that specific one. Now notice what
happened. It comes back and says there's
nothing, and that's because the jobs have
completed and there's no jobs to be able
to come back. So just be aware that when
you have jobs backwards and forwards, it's
often quite hard to remember where they
are. You think they may be local, but
actually they're more external. Now, the
other option is to create remote
background jobs on either single or
multiple computers. So what we can do this
time is, we can use the same principle
where we say New‑PSSession ‑ComputerName.
I'm going to type my IP address, and I'm
going to pass my credential because I know
I'm going to get asked for that, okay, and
password. Okay, now if I just do session,
you can see it gives me my session that's
opened. What I can then do is actually run
Invoke‑Command. I'm now going to inject
into the current session my ScriptBlock,
which is the same as before, Process, and
then notice what I'm going to do now. I'm
going to pass that as an AsJob statement.
Now notice what happened the last time. We
did Invoke‑Command, we didn't use AsJob.
We tried to get the job and there was no
job. And that's because we didn't pass in
the AsJob. Now there we have the jobs that
have come back. Now, let's just do Get‑Job
from here, okay. You can see that it's now
listing me the jobs that have been
initiated, and so I could now manage those
as I would any other job. So to initiate a
remote background job, it's as simple as
either invoking the command directly to
the machine that you want to put the job
on or simply passing in a New‑PSSession
object with a series of computers or
single ones and then invoking the command
as a Start‑Job. Now we also have the other
approach that we can utilize, which, if we
were to go back to our command here and
not use that, we could actually come in
and say, Start‑Job. Let me get rid of all
of my Get‑Process. And let me do name
ScriptBlock. And notice how we can start
to nest pieces in here, so I'm going to
say Get‑Process. So what we've now done is
initiated a new invoke‑Command. The
ScriptBlock for that is to start a job,
and then the Start‑Job is going to kick of
to Get‑Process. So now if I put Get‑Job,
notice locally I only get Job3, so where
is Job1? Well, Job1 exists on that other
machine. So just be aware that it can be
quite confusing when you're creating the
remote background jobs of where those jobs actually get created.