Okay,
so the backup is now completed. Our next task now that we can
run a script from a remote environment or a share is to do
something a bit more sophisticated.
So what about if we have an environment where we need to copy files to it,
but we can't actually get to it from a UNC path approach for
various security protocols or restrictions? Well,
let me just clear this here.
And what we'll do is we'll set some variables up.
So the first thing here is to set a location variable, and we'll set this
to the Active Directory server where all the shares are.
And we actually have a directory here called Files.
So let's go back to my folder structure, and we'll go here, and we'll click
onto my server, and we'll click Files. And sure enough,
it has a directory in here called PSTools, so these are the
Sysinternals tools, and we want to make sure that we can copy
those to each of the machines that we create sessions to or
somehow get them across to those machines.
So let's go back to our PowerShell. So the next thing I want to
do is set the destination of where we want those files to
actually be stored on that other machine.
So we'll do it nice and simple. We'll create a directory called
C:\Files, and that's where we'll store them.
So we have our location and destination.
Now let's do this manually to begin with.
The first thing we can do is utilize something called
Copy‑Item, and Copy‑Item is just a standard PowerShell
command, so we can say ‑Path here.
What I can then do is put in the location variable that we already
created, and then I can do a \*, which will represent every single file in
that directory, that PSTools one. I can then set the ‑Destination to be
whatever the destination is going to be,
which in this case, will be the local machine that I'm on in the
C:\Program Files location, so that Files directory I've got.
What I'm then going to do is tell it to recursively do this.
So I'm going to press Enter.
It's going to go ahead and connect to the remote location,
grab whatever the files are, and then it will have
copied it, but notice, no response here.
So let's go back to my Files directory, and sure
enough, on my C: drive, Files, PSTools,
there's the list of files. So it copied them from the
network share to my machine, so that's great.
That means we just need to have a script that executes on
each machine and copies everything over.
But what about if there was a better way?
What about if we could create a session that connects to a machine?
So let me clear this a little bit. We're going to make some changes to this.
So what we're going to do is say $session1, and we'll say
New‑PSSession like so. We'll say ‑ComputerName, and I'm going to use
the IP addresses of the machines that we've got.
I'm going to set my authentication to be Credssp, and then I'll set my
‑Credential to be $creds, and so I'm creating a new session.
I'm now going to go back and modify the session to go to the second machine,
and we'll create a new variable, and I'll do a third one, which will be to
our third member server that we've got as well.
So I'm creating three PowerShell sessions.
If I now say Get‑PSSession, you'll see I've got a whole list of sessions
there, but you see I've got a bunch for number 10, but the most important
ones are 73, 74, 75, and I've got them to 10,
11, and 12, so I have some PowerShell sessions.
In actual fact, what I'll do just to make sure that we
don't send it to the wrong ones,
I'm going to say ‑Id of 70, 71, and 72, and then we'll just reissue the
PSSession, and there we go. We've got our clean three environments.
Now the idea is here that we need to copy those files to those machines,
but have no way of doing it.
So if we go back to the Copy‑Item syntax that we had,
so we're going to use the same process again.
Now the location is going to change because, obviously, we
may be coming from that Files directory or we may be pulling
stuff from a different location.
The destination might be different.
We may be saving it to a different directory,
but for the purposes of this, we'll use all the same values like
so. So we'll say "$($location)\*, which means it's going to be
everything in that PSTools directory.
I'm going to set the destination to be exactly the same on every single machine.
And then, of course, we're going to recursively do
this. Now if I was to enter here, it would literally do it locally,
but luckily for us, we have another property called ‑ToSession.
So I'm going to say $session1, $session1 is our training server number 1.
I'm going to press Enter. Now you can see, it's copying a
bunch of files, and it says From to 10.0.0.10.
So it's now copying them to the other machine.
If I go and repeat that to number 2,
you can see 10.0.0.11, so these are different machines that it's copying to.
And then if we do number 3, do the same thing,
it's now going to copy those files through the PowerShell
session without using UNC connections or trying to browse to
it, and it's going to copy those files.
So now let's flick over to one of those machines. We'll go
to the $session3, so that's our third server, and we'll
make sure those files are there.
So this is our server 3.
If I now click my folder structure here,
and click here, C: drive, Files,
you'll see that all of those files are copied to this machine over here. So we
can use the same syntax, but using that ToSession option,
it will allow us to copy the files from the one location to the other.
Now, of course,
the key to everything here is that when we create the
sessions on the other machines,
in order to get around any of the security issues that you may face, we do need to look at potentially utilizing the Credssp approach as well.