Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install powershell module in closed network

Tags:

powershell

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?

like image 435
Wojciech Szabowicz Avatar asked Sep 17 '26 20:09

Wojciech Szabowicz


2 Answers

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.)
like image 75
Eddie Kumar Avatar answered Sep 19 '26 18:09

Eddie Kumar


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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!