All of this capability to discover and
install modules from the online PowerShell
Gallery is absolutely fantastic, but what
if you have a scenario where there's a
machine that doesn't have connectivity to
the PowerShell Gallery or no internet
connectivity at all, and you want to
install a PowerShell module on that
machine? This can also happen in dark
sites or offline environments such as
ships, oil rigs, secure locations, there's
plenty of examples. There's a command
included in the PowerShell Get module
named Save‑Module, and you can use this on
a computer that is connected to a
repository such as the PowerShell Gallery,
to save the module without actually
installing it. For me, this command
actually has two primary use cases. The
first one is that because Save‑Module
doesn't actually install the module, this
gives you the capability to pull down the
code for that module in question. You can
then inspect the code within the module to
see how it's been written to get a feel
for whether you are happy to actually
install this module or not. Publishing to
a repository like the PowerShell Gallery
does have guidelines, and they are
scanning it for malicious code, but it can
never hurt to check it for yourself to
ensure that you are comfortable with it.
The second use case for Save‑Module is to
download a copy of the module in question
without installing it, providing you with
the package that you can then transfer to
that offline machine using, well,
basically any method you can to move
files. It might be across a network share
or on a USB drive. When that package has
been copied to the destination computer,
you can then literally copy and paste the
folder into one of the PS module paths on
that offline machine, and then you can
start using that module. Let's go through
that process now. The help here shows us
that we have the same parameters as other
commands to be specific about the
versioning, but this time we've got two
required parameters, which are ‑name for
the name of the module we want to save,
and ‑path to specify which path we want to
save the module to. On this computer, the
one that we've been using for the duration
of this course so far, I'm going to create
a new directory in the C:\ drive called
offlinepsmodule, and I'll use the
‑ItemType parameter to ensure it gets
created as a directory. I can then use
Save‑Module and specify the name of the
module, which for this demo we'll just use
the PSSlack module again and also provide
the path in which I want to save it to,
which is the folder that I just created.
Let's check the contents of that folder by
using Get‑ChildItem, and you'll note that
there's now a single folder there named
PSSlack. Let's look inside of that folder
and there's a subfolder with the version
of the module 1.0.5, being the latest that
it saved from the PowerShell Gallery.
Let's check out the contents of that
folder, and this is the underlying code
and components that form the PowerShell
module. I'm going to flick across to
another machine that I have here, which
I've got set up with a blue PowerShell
console so you can tell that we've
switched across. This machine is an exact
clone of the machine that we've been
working on throughout this course so far,
with the exception that this one does not
have a network card at all, and it's got
no connectivity to any network, let alone
the internet and the PowerShell Gallery.
Let me do an ipconfig here, and you'll see
that there's no data returned. And if I
try and ping a popular address like
1.1.1.1, it'll fail. Let me try and
perform a Find‑Module for PSSlack, and
you'll see we get this warning at the top,
telling me that it's unable to resolve the
PowerShell Gallery. There's no way I'm
going to be able to install anything from
the PowerShell Gallery if I can't even
resolve the DNS entry. Just so you know
that there's no smoke and mirrors here,
I'll also run Get‑Module PSSlack
‑ListAvailable to show that it is not
found in any of the PS module path
folders. Now all I have done here is
copied that folder, offlinepsmodule, from
the first computer we were on a few
moments ago, and I've copied it to this
computer in the same location. Now you
might be thinking we can use
Install‑Module, but Install‑Module is a
part of PowerShell Get, which interfaces
with repositories, and we don't have
access to any repository. So to get this
module working on this computer, I'm going
to simply use Copy‑Item to copy the
PSSlack folder from C:\offlinepsmodule,
and I'm going to copy it to one of the
default PS module paths. So I'll pick the
one down in my user profile, which is at
C:\Users\Matt\Documents\PowerShell\ and
then Modules. Because we want to ensure
that everything is copied, I'll use the
‑recurse parameter and then let that copy
across. If I run Get‑Module again to see
if it finds the PSSlack module, this time
we get a result back, which is great. From
there, I could import the module into this
session, and I can use Get‑Command to list
the commands inside of this module and
start using it on this computer that has
no network connectivity to the PowerShell Gallery.