Last active
December 15, 2020 08:12
-
-
Save Jaykul/9a810bac8584dd654cf9b0cffe6426eb to your computer and use it in GitHub Desktop.
PowerShell Gallery Module - Light
This file contains 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 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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I borrowed some of this code for a script to refresh the "community" modules in my private NuGet Repository.
Thanks for sharing!