Skip to content

Instantly share code, notes, and snippets.

@amandadebler
Forked from Jaykul/PowerShellGet.psm1
Last active October 16, 2019 18:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amandadebler/ecb3f6ea79ec939acdf12e25710040e1 to your computer and use it in GitHub Desktop.
Save amandadebler/ecb3f6ea79ec939acdf12e25710040e1 to your computer and use it in GitHub Desktop.
PowerShell Gallery Module - Bootleg Edition
<#
Forked from https://github.com/jaykul Gist, posted in response to my Twitter
whinge about PowerShellGet in the enterprise - Thank you, Joel Bennett!
This adds the ability to use a web proxy, using your current session's
credentials. Might add option to use other credentials, if there's demand.
It also adds a 'BL' (Bootleg) prefix to the nouns, just to prevent confusion with the "real"
cmdlets. Feel free to remove them.
ToDo: Use parameter sets :)
Examples:
Get Chrissy LeMaire and Co's outstanding dbatools for inspection:
Find-BLModule dbatools | Save-BLModule -Destination c:\temp
Get dbatools for inspection if you're stuck behind a proxy the requires your Windows credentials:
Find-BLModule dbatools -Proxy http://proxy.mandie.net | Save-BLModule -Destination c:\temp -Proxy http://proxy.mandie.net
#>
function Find-BLModule {
<#
.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,
# Your web proxy, if any (use full "http://..." address)
[String]$Proxy
)
if ($Proxy) {
$ProxySettings = @{
Proxy = $Proxy;
ProxyUseDefaultCredentials = $true
}
}
# 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-Object { "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
# Should totally have used parameter sets. Whatever.
if ($proxy) {
$result = Invoke-RestMethod -Uri $url @ProxySettings
} else {
$result = Invoke-RestMethod -Uri $url
}
$result | Where-Object { $_.title.'#text' -like $Name } |
Select-Object @{n='Name';ex={$_.title.'#text'}},
@{n='Version';ex={$_.properties.version}},
@{n='Url';ex={$_.Content.src}}
}
function Save-BLModule {
<#
.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,
# Your web proxy, if any
[String]$Proxy
)
begin {
if ($Proxy) {
$ProxySettings = @{
Proxy = $Proxy;
ProxyUseDefaultCredentials = $true
}
}
}
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"
if ($proxy) {
Invoke-WebRequest -Uri $Url -OutFile $Path @ProxySettings
} else {
Invoke-WebRequest -Uri $Url -OutFile $Path
}
Get-Item $Path
}
}
function Install-BLModule {
<#
.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