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