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?
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:
Save-Module -Name "SQLServer" -Path C:\SavedPSModules
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).
#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:
$env:PSModulePath -split ';'
Mkdir C:\Users\{user_name}\Documents\WindowsPowerShell\Modules;
Let me give you two approaches in this case.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With