Now,
an interesting use case that we often find when working with
PowerShell is the ability to move files,
and objects, and content around.
Now copying files can be done either manually or you can do script
execution and you can use a network share for example.
So you know, maybe we're using the Active Directory server share,
have some files in there, and we need to copy them from one place to another.
We also can use script execution for remote PowerShell as well.
So we can either manually copy files, write PowerShell to copy files,
or what would be helpful is if I need to copy,
let's say,
I want to copy a series of PowerShell script files to the
remote server to execute them and do that every time we
connect to make sure they're updated, then,
of course,
I don't want to be manually having to copy six files over to here
because what happens if you are in a secure environment and don't
actually have access to UNC or shared paths.
To achieve this, we can use the Copy‑Item command.
This will allow us to copy an item from one location to another.
Now the cmdlet doesn't cut or delete the items, it literally just copies.
You're able to copy and rename items as part of the same command.
You can use it locally or remotely, and to do it remotely,
we can utilize ‑ToSession to work within the construct of remote computers.
So what does this look like?
Well, let's have a look.
So, I have a location and a destination.
So my location is on my Active Directory server,
I have a Files share, and then my destination is I wish to store
them locally in a new folder called C:\Files.
We can use Copy‑Item, so Path, which would be the location,
\* because I want to move everything, and then I have the Destination option,
which lets me just say dump them over there.
So I can run this on my local machine.
So I could go into my admin workstation,
do the same process, and say run that command,
and it will copy the files from the network share
into that folder on my machine.
Now if you want to copy the files to a remote computer
that you may not have UNC access to, then,
of course, we can create a session first.
So we're going to use New‑PSSession,
which is the Active Directory server at this point.
I'm going to say Copy‑Item with a Path of the files to get to,
set the Destination,
and then you'll see at the end there we use ToSession and we tell it to
send this command down the session that we created.
So this means I can copy files from one machine through an existing
PowerShell session and have them pop out on the other machine without
actually having to have UNC or shared network access.
So what's the use case for using this?
Well, Globomantics is a highly secure environment,
so for example, there's no network copying to specific servers.
Maybe there'll be specific port restrictions as well.
The core requirement is to copy custom PowerShell
scripts to the remote computers.
So that's the objective here, we want to be able to say,
well those three member servers, we can't really copy things to them,
but I want to somehow get those files to it.
And then, of course,
the requirement is for me as an admin to copy things like
administration programs to the remote computers.
So maybe we're trying to copy pieces that are part of,
for example, Sysinternals, tools that we wish to utilize remotely.
We can't get them to the machine, so we want to use PowerShell to be able to push them over there.