3

I have a simple question but yet I can not find anywhere answer for it.

I am using powershell 5 and I'm working in closed environment, without ability to connect to internet, I want to install on my machine manually module, basically any module that is available to download for example posh-ssh.

Can it be done? Lets say save module here and install?

Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • 2
    It is literally called `Save-Module`. http://stackoverflow.com/questions/37486587/powershell-v5-how-to-install-modules-to-a-computer-having-no-internet-connecti – 4c74356b41 Dec 13 '16 at 13:40

2 Answers2

8

As @jyao asked for some examples, here is my answer with examples/sample code:

The easiest way to install/make the PowerShell Modules available in a closed network (or in an offline computer), is to use an online computer to download the required modules first, and then save/copy those modules into a location which is accessible from within the closed network.

STEPS:

Note: I'm using "SQLServer" module in the following example:

  • Logon to the computer which is online (and in which downloading is not blocked by the Firewall.)
  • Save the required module (in a local folder), e.g.:

Save-Module -Name "SQLServer" -Path C:\SavedPSModules

  • Now, copy the module into the offline computer (in one of the Modules-folders where PowerShell looks for Modules), e.g.:

Copy-Item "C:\SavedPSModules\SQLServer" -Destination "C:\User\{usr_name}\Documents\WindowsPowerShell\Modules" -Recurse -ToSession $session; #assuming target computer is within accessible domain/network, to which you'll need to create a Session (here stored in $session variable).

  • Finally install + import module:
#Check if module is installed & imported, do it if not:
$module = "SQLServer"
if((Get-Module -Name $module) -eq $null) {
    #Check if module is installed:
    if (Get-Module -ListAvailable -Name $module) {
        Write-Output "$module module is already installed";
        #Check if module is imported:
        if((Get-Module -Name $module) -ne $null) {
            Write-Output " and is already imported (i.e. its cmdlets are ready/available to be used.)";
            Write-Output "Installation Path: `n`t" (Get-Module -Name $module).Path;
        } else {
            Write-Output ", however, it is NOT imported yet - importing it now";
            Import-Module $module;
        }
    }else{
        Write-Output "$module module is NOT installed - installing it now"
        try {
            Install-Module -Name $module -AllowClobber -Confirm:$False -Force  #suppressed prompt.
        }
        catch [Exception] {
            $_.message 
            exit
        }
        Write-Output "$module installed successfully - importing it now";
        Import-Module $module;
    }
}

Notes:

  • The $env:PSModulePath variable holds the locations where PowerShell looks for the installed modules, if you want to keep your modules in a shared network folder, simply append its location to this variable. To list currently set Modules-folders, use following:

$env:PSModulePath -split ';'

  • If "Modules" folder doesn't exists, you may need to create it, e.g.:

Mkdir C:\Users\{user_name}\Documents\WindowsPowerShell\Modules;

  • Remember: Find-Module cmdlet (and its sibling-cmdlets such as Save-Module & Install-Module) will only work if the downloading is enabled in your server (i.e. your computer is connected to the internet & downloading is NOT blocked by the Firewall.)

HTH

Eddie Kumar
  • 1,216
  • 18
  • 20
3

Let me give you two approaches in this case.

  1. You can download all the modules whatever you need from the gallery and keep it in some share box in one of your systems. Then just install the modules from that share folder. So it will act as a separate repository for you.
  2. You can create an internal FTP and keep it over there . Then using powershell you can download all of them easily and it will be accessible also in the closed network.

Hope it helps.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • 1
    It would be better if you can give an example because currently, we use install-module to install a PS module, so if you have a module in a shared folder, how can I use install-module to access that shared folder? – jyao Apr 23 '18 at 18:28
  • You need to Give the full path of the `.psm1`(module) file in the `Install-Module` command. Further, you can use `Save-Module` to save the modules locally. Refer [THIS](https://stackoverflow.com/questions/41585758/install-module-the-term-install-module-is-not-recognized-as-the-name-of-a-cm/41586317#41586317) link... – Ranadip Dutta Jun 30 '18 at 16:45