So let's go back into our Globomantics environment.
We'll look at how we copy files to remote computers.
We'll first create the remote PowerShell sessions that are required.
Then, we'll execute the PowerShell scripts that are
stored in that network share.
And then we'll lastly look at how we copy files
directly through that remote session.
So now that we've been able to connect to the multiple machines and execute
PowerShell scripts, what we want to do now is look at how we can get things
to or files or objects copied to other machines.
Now the first thing I want to do is obviously just remind us of how
we make connections across to other locations.
So the first thing I want to do is create a specific
session to one of the remote machines.
So I'm going to say New‑PSSession, and then, of course, if
we go through our ‑ComputerName option, what we'll do is we'll use 10.0.0.10,
which is one of our main machines, and we'll just click Enter.
And, of course, remember if we get this error, we do need to make
sure that our ‑Credential object is specified as $creds, and then
that gives us our connection.
So if I click $session, you can see I have a remote session that's available.
Now, if you look carefully at the command,
you remember that we created, obviously, a new
PowerShell session using ‑Credential.
Now, if we clear this, well, before we clear that,
tell you what,
let's just remove the PSSession that we created, and we'll
say, Id of 69, and we'll get rid of that.
Let's go back and look at how we can create that new PowerShell
session and if we can pass Credssp to it because remember, we want to
be able to execute the command on the other side, and we want to get
past any of the security issues.
So I'm going to say Credssp, so this is now going to
create me my session. And sure enough,
I've now got my session opened to that machine.
So once we have that, we can then obviously invoke or
execute commands on the other machine.
So let's go back to our folder structure of scripts that we
have. We originally looked at the Report option,
but what about if I wanted to enable or perform a backup?
So let me just right‑click on one of these files, and we'll
say Open with Code. We'll say Open, so we can see. Now in the
Install‑Backup PowerShell script,
I want to install Windows‑Server‑Backup, and then
force a restart on that computer.
So this is utilizing PowerShell. Now what we can do is
we could capture this command here,
so I'm going to say Copy. We now have the session, so let's just
invoke that command that we had in the PowerShell.
Now, of course, we can reuse this existing session.
So instead of saying ‑ComputerName, I can say ‑Session.
Now, of course, when we utilize the session,
we don't need to pass any credentials or anything like that because that was
created as part of the initial session. We can then do ‑ScriptBlock, and I can
paste my install backup, and I can press Enter.
Now this has now gone ahead, and trying to execute that over there. It
already says success because I've already run this in the past,
so we didn't have to wait for it to run,
but that command installs Windows‑Server‑Backup.
So let's go back to my Visual Studio Code. My next command would be to restart,
which I don't want to restart, so we can imagine that that's been done.
So let's then go back to my Scripts directory, and we'll choose Backup,
and we'll say Open with Code, and we'll open this again.
And now this one's a little bit different.
This one is going to retrieve the name of the computer, then set the path.
Notice we're using the Active Directory server and a folder called Backup.
So let me go into here and find that directory, so Backup, so
there's my empty folder. And what it's going to do is go ahead
and create a directory for this path,
which will be basically the name of the machine in there.
And then it will go ahead and create the policy, backing up the C:
drive, and then storing that as a backup option.
So, the use case here is that we want to run this script on a regular basis.
So let's go back into here.
And what we'll do is we can say Invoke‑Command, and I'm going to get
rid of my ‑ScriptBlock here because we're going to use the ‑FilePath
one. I need to put my ‑FilePath option. And then in the ‑FilePath, we
can get the UNC path to that script.
So let's go back to my Scripts one, and what we'll do here is say Backup.
I'm going to select the UNC path, and then go here and paste
that in, and then I can execute that command.
Now this will go ahead. Notice, it's created my
directory called TRAININGSRV01VM.
So let's go back to my folder structure. We'll go back to 10.0.0.5 Backup.
And sure enough, that folder is created. And if I waited and watched it,
it would then start to load values into there.
So this is now going to perform a backup of this machine
directly into the Active Directory server.
Now you might have to give it a few minutes to wait, but there you go.
It just appeared, WindowsImageBackup,
and it will continue to run as we need it to, so a fairly painless process.