Created
September 25, 2017 13:53
-
-
Save JPRuskin/ea2e54c6894a1293cd4f8f0a93e85205 to your computer and use it in GitHub Desktop.
Download modules from PSGallery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Download-Module { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] | |
[String[]]$Name, | |
[Parameter(ValueFromPipelineByPropertyName)] | |
[String]$Repository, | |
[PSCredential]$Credential, | |
[IO.DirectoryInfo]$OutDirectory | |
) | |
begin { | |
$ModuleParams = @{} | |
if ($Repository) {$ModuleParams.Repository = $Repository} | |
if ($Credential) {$ModuleParams.Credential = $Credential} | |
# TODO: This currently breaks on non-PSGallery repos. Fix that by not using Find-Module. | |
$Name | %{ | |
$ModuleParams.Name = $PSItem | |
[Array]$Modules += Find-Module @ModuleParams | |
} | |
if (Test-Path $OutFile -PathType Container) { | |
$OutDirectory = $OutFile | |
Remove-Variable OutFile | |
} elseif ((Test-Path $OutFile -PathType Leaf) -and $Modules.Count -gt 1) { | |
$OutDirectory = Split-Path $OutFile -Parent | |
Remove-Variable OutFile | |
} else { | |
$OutDirectory = $PWD | |
} | |
} | |
process { | |
foreach ($Module in $Modules) { | |
if (-not $OutFile -or $Modules.Count -gt 1) {$OutFile = Join-Path $OutDirectory "$($Module.Name).$($Module.Version).nupkg"} | |
$DownloadParams = @{ | |
Uri = (Get-PSRepository -Name $Module.Repository).PublishLocation + "/" + $Module.Name + "/" + $Module.Version | |
OutFile = $OutFile | |
} | |
if ($Credential) {$DownloadParams.Credential = $Credential} | |
Invoke-WebRequest @DownloadParams | |
[PSCustomObject]@{ | |
Name = $Module.Name | |
Version = $Module.Version | |
Path = $OutFile | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment