So how do we create remote background
jobs? Well, this is done first by
executing the computer name. So we'll need
to pass a computer name as part of the
command and say, I want to talk to domain
controller 1, SQL server 2, etc. We then
have an optional parameter, which will be
the credential. So we'll say, domain/user
to that machine or whatever account it
needs to be. And then, of course, we have
the script block parameter, which is what
we wish to execute on that remote machine.
So some examples of this, if we wish to
run a command on a remote computer, we're
going to use the Invoke‑Command again,
we'll pass the computer name, we'll pass a
credential this time. Which you may not
have to, because if you're on a member
server, it's part of a domain. And you're
already logged in with valid credentials
that would allow you to log into the other
machines. You won't ever have to pass
credential, but sometimes you may need to.
And then, of course, our ‑ScriptBlock is
the execution wish to send. So we're going
to say go to that computer and retrieve
the Get‑Process. The other option is we
can create remote background jobs on those
computers by using a different mechanism.
So instead of me running on one single
machine, I'm going to say, execute this on
multiple machines. So I'm going to say, go
and retrieve me a new non‑interactive
PowerShell session for these specific
machines. And then I'm going to say,
invoke the command, and you'll notice I'm
now using ‑AsJob. So in the first example,
I'm just executing a command. I'm not
really creating a job. But in the second
one, I'm using that ‑AsJob parameter to
change what was originally created to be
jobs now, inside those PowerShell sessions.