So once we have either a single session or multiple sessions,
how do we execute commands on those remote computers?
Well, for a single remote session we're able to go,
let's say I have my admin workstation here and I've got my member server,
so in that single remote connection I'm able to pass
commands backwards and forwards dynamically through what's
referred to as the WSMan connection.
The WSMan connection is that remote connection between the two machines.
So you can see on the left‑hand side in the
workstation I simply type some commands,
they ran across to the member server and then came back.
So how does that differ for the remote servers?
Well, let's look at our diagram a little bit differently.
So we obviously have the Admin Workstation,
and of course we have Member Server 01 and 02,
and you'll see that we have a WSMan connection as well.
So I issue a command, it runs from the one machine to the other,
and then it runs across to the other machine as well.
So,
in a multiple remote session, you execute the commands from the
single entry point on the admin workstation,
and it will send those to the remote machines and then return values back.
Now in order for us to execute remote commands,
we need to understand the format or the syntax format that we need to utilize.
The first piece of this is that we're going to use Invoke‑Command,
the cmdlet that will say go and send this command from here to somewhere else,
whatever that remote machine would be.
We can also then use the script block or file path,
as we've looked at previously,
so either in curly brackets send a PowerShell cmdlet that already exists or a
UNC path to a PowerShell script that we've already created.
And then lastly we can send any optional arguments and parameters.
So, for example,
maybe we want to send a variable value that we've
populated and push that down that command.
So we have the ability to create very complex commands that
can be ran from the admin workstation and pushed out remotely
to all of the other machines.
So, let's walk through a sample PowerShell script,
because the objective here is for us to be able to
execute PowerShell remotely on machines.
So the first thing is here,
I want to set a location. So the location is going to be a UNC path,
and you may wonder why we're using that.
We're using a shared location because I need somewhere where
all of the machines can gain access back to,
because I want to write some values back.
What I'm then going to do is I'm going to set some desktop
settings here in the PowerShell session.
So I'm going to go and get the name of the current computer,
and if the environment variable for COMPUTERNAME is actually empty,
then I'm going to use the CimInstance command to get that, and then I'm going
to go and retrieve information about the desktop itself.
I'm also then going to retrieve the computer manufacturer details.
Notice we're using PowerShell, just regular CimInstance commands to get this.
And then I'm going to get the operating system version
information as well, so I'm going to be using some commands
called the CimInstance commands.
This just allows me to query information on Windows
devices and kind of present that back.
So that's our first bit.
So we're setting the Save To Location,
which means I'm going to get this info and write it back somewhere, and
I'm going to write it back to a shared location, and then I'm going to
retrieve info about about the servers.
Part two of this is to actually create the report file.
So I'm actually going to say create me a report, I'm going to use the
location variable that we already set, which is the shared drive, and then
I'm going to create a report passing in the name option,
which is a variable that we populated in the previous one.
And then I'm going to say create me a new report,
call it Device Report, and then you can see I'm adding content to this file, so
this log file will have a section called Desktop Details,
Manufacturer Details, and Operating System Details, and then
write the values from the executed commands.
Now, if we look at how this works,
if I'm using a single kind of remote session, then what I can do
is actually obviously invoke the command and say this is my
computer and then run this information,
which is great, but we're using a PowerShell script so
this is going to look differently.
So you can see from here that my script, notice that I'm
using a shared location to get the script,
remember that if you've got a script on your machine, trying to execute it
on a remote machine is a bit more complicated because the script doesn't
have a path to execute because it's on my machine,
not, obviously, on the other machine.
So to get around this we can actually create a file share,
store the ps1 file or the PowerShell script, and then
call that script in the Invoke‑Command.
So that means, for example,
I'm connecting to the Active Directory server here, and I'm setting the
FilePath to the UNC or shared path of the PowerShell script and having it
execute there. Now if we're doing multiple sessions,
because obviously this is the ideal approach here, the first thing
we'll do is define our set of computers again,
so you can see I'm using the same variables,
so I'm using my Member Server 01, 02, and 03 IP addresses.
I can then say invoke the command if I'm using a ScriptBlock,
which we've seen before.
So how does this change with the ps1? Well, we simply say script equals, back
to that same UNC path, and then we just use Invoke‑Command.
So Invoke‑Command, ‑ComputerName,
pass in the object that contains all the computers and the FilePath to the UNC, and then it will go ahead and execute that script.