PowerShell v5 - How to install modules to a computer having no internet connection?

不羁岁月 提交于 2019-12-09 16:51:12

问题


I've a machine (v3, internet, no admin access) which I used to download WMF 5.0 and set up another machine(v5, no internet, admin access). Now, I want to use some modules from PowerShellGet on the machine running v5 but no internet connection.

I need an option to download *.psm1 file which I can then copy over and use. Just like we have options to download from GitHub.

Anyone with a similar issue and any workarounds ?


回答1:


Install the Package Management Module on your PowerShell 3 machine, and then use Save-Module ...

Or set up ProGet somewhere "on the edge" of your network, and have it mirror the modules you want from the public PowerShellGallery for your internal-only clients.

Failing that, just build your own download URL:

https://www.powershellgallery.com/api/v2/package/$Name/$Version

You can even generate an OData proxy module, or just use invoke-restmethod to search:

function Find-Module {
    param($Name)
    invoke-restmethod "https://www.powershellgallery.com/api/v2/Packages?`$filter=Id eq '$name' and IsLatestVersion" | 
    select-Object @{n='Name';ex={$_.title.'#text'}},
                  @{n='Version';ex={$_.properties.version}},
                  @{n='Uri';ex={$_.Content.src}}
}
function Save-Module {
    param(
        [Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
        $Name,
        [Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true)]$Uri,
        [Parameter(ValueFromPipelineByPropertyName=$true)]$Version="",
        [string]$Path = $pwd
    )
    $Path = (Join-Path $Path "$Name.$Version.nupkg")
    Invoke-WebRequest $Uri -OutFile $Path
    Get-Item $Path
}

So now you can just do the same as with the official module:

Find-Module Pester | Save-Module -Path ~\Downloads



回答2:


Update your machine with internet access to PowerShell 5.0 and use Save-Module to save modules from PowerShellGet. Ex:

Find-Module psreadline | Save-Module -Path c:\users\frode\Desktop

This will save the module (ex. PSReadLine) to a folder which you can copy to your other machine and install like a normal module (see Installing a PowerShell Module)



来源:https://stackoverflow.com/questions/37486587/powershell-v5-how-to-install-modules-to-a-computer-having-no-internet-connecti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!