Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active December 15, 2020 08:12
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Jaykul/9a810bac8584dd654cf9b0cffe6426eb to your computer and use it in GitHub Desktop.
Save Jaykul/9a810bac8584dd654cf9b0cffe6426eb to your computer and use it in GitHub Desktop.
PowerShell Gallery Module - Light
function Find-Module {
<#
.Synopsis
A wrapper for Invoke-RestMethod to search the PowerShell Gallery
.Description
In order to support wildcards, we build pretty complicated URLs,
and then we filter the results by title
#>
[CmdletBinding()]
param (
# The module name (supports the * wildcard)
[string]$Name
)
# We can support wildcards by splitting, searching for each piece, and then filtering the results
# Build a URL using substringof
$filter = @($Name.Trim('*').Split('*') | ForEach { "substringof('$_',Id)" }) -join " and "
$url = "https://www.powershellgallery.com/api/v2/Packages?`$filter=$filter and IsLatestVersion"
# Fetch results and filter them with -like, and then shape the output
Invoke-RestMethod $url | Where { $_.title.'#text' -like $Name } |
Select-Object @{n='Name';ex={$_.title.'#text'}},
@{n='Version';ex={$_.properties.version}},
@{n='Url';ex={$_.Content.src}}
}
function Save-Module {
<#
.Synopsis
A wrapper for Invoke-WebRequest -OutFile to save modules with the nuget package file names
#>
[CmdletBinding()]
param (
# The Url to download from
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
$Url,
# The name of the module (for naming the output file)
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
$Name,
# The version of the module (for naming the output file)
[Parameter(ValueFromPipelineByPropertyName=$true)]
$Version="",
# The folder to save to
[Alias("Path")]
[string]$Destination = $pwd
)
process {
if($Destination -eq "CurrentUser") {
$Destination = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell\Modules"
}
if($Destination -eq "AllUsers" -or $Destination -eq "LocalMachine") {
$Destination = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "WindowsPowerShell\Modules"
}
if(-not (Test-Path $Destination)) {
$null = mkdir $Destination -force
}
$Path = Join-Path $Destination "$Name.$Version.nupkg"
Invoke-WebRequest $Url -OutFile $Path
Get-Item $Path
}
}
function Extract-Module {
<#
.Synopsis
A wrapper for Extract-Archive to unzip modules from nuget packages
#>
[CmdletBinding()]
param (
# The Url to download from
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("PSPath")]
$Path,
# The folder to save to
[string]$Destination = "AllUsers"
)
process {
if($Destination -eq "CurrentUser") {
$Destination = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell\Modules"
}
if($Destination -eq "AllUsers" -or $Destination -eq "LocalMachine") {
$Destination = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "WindowsPowerShell\Modules"
}
if(-not (Test-Path $Destination)) {
$null = mkdir $Destination -force
}
if([IO.Path]::GetExtension($Path) -eq ".nupkg") {
$Path = Rename-Item $Path ([IO.Path]::ChangeExtension([IO.Path]::GetFileName($Path), ".zip")) -Passthru
}
$Module, $Version = $Path.BaseName -split "\.",2
Expand-Archive $Path -Destination (Join-Path $Destination (Join-Path $Module $Version))
}
}
@abbgrade
Copy link

Thats really nice! I added Powershell 4 support and a method to get the required modules.
https://gist.github.com/abbgrade/70a1481422c1e5e22c43f2f4df6896ce

@AspenForester
Copy link

I borrowed some of this code for a script to refresh the "community" modules in my private NuGet Repository.
Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment