Skip to content

Instantly share code, notes, and snippets.

@abbgrade
Forked from Jaykul/PowerShellGet.psm1
Last active August 29, 2018 13:53
Show Gist options
  • Save abbgrade/70a1481422c1e5e22c43f2f4df6896ce to your computer and use it in GitHub Desktop.
Save abbgrade/70a1481422c1e5e22c43f2f4df6896ce to your computer and use it in GitHub Desktop.
PowerShell Gallery Module - Light
<#
.Example
# Download and install MicrosoftPowerBIMgmt with the required modules.
Get-RequiredModules Find-Module -Name 'MicrosoftPowerBIMgmt' -Verbose | Save-Module -Verbose | Extract-Module -Destination 'CurrentUser' -Verbose -Force | Foreach-Object {
Write-Verbose "Install $( $_ )" -Verbose
Find-Module -Name $_ -Verbose | Save-Module -Verbose | Extract-Module -Destination 'CurrentUser' -Verbose -Force
}
#>
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"
if ( Test-Path $Path ) {
Write-Verbose "Download skipped. Module '$Path' already exists."
}
else {
Write-Verbose "Start download of module '$Path'."
Invoke-WebRequest $Url -OutFile $Path
}
$result = Get-Item $Path
$result | Add-Member ModuleName $Name
$result | Add-Member ModuleVersion $Version
Write-Output $result
}
}
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,
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("ModuleName")]
$Module,
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("ModuleVersion")]
$Version,
# The folder to save to
[ValidateSet('AllUsers', 'CurrentUser', 'LocalMachine')]
[string] $Destination = "AllUsers",
[switch] $Force = $false
)
process {
[string] $ModulesPath = $null
switch ( $Destination ) {
"CurrentUser" { $ModulesPath = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell\Modules" }
"AllUsers" { $ModulesPath = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "WindowsPowerShell\Modules" }
"LocalMachine" { $ModulesPath = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "WindowsPowerShell\Modules" }
}
if(-not (Test-Path $ModulesPath)) {
New-item -ItemType Directory -Path $ModulesPath -Force | Out-Null
}
$Package = Get-Item $Path
if ( $PSVersionTable.PSVersion.Major -ge 5 ) {
$ModulePath = (Join-Path $ModulesPath (Join-Path $Module $Version))
} else {
$ModulePath = (Join-Path $ModulesPath $Module)
}
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null
if ( $Force -and ( Test-Path $ModulePath ))
{
Remove-Item $ModulePath -Recurse
}
[IO.Compression.ZipFile]::ExtractToDirectory( $Package.FullName, $ModulePath )
Get-Item $ModulePath
}
}
function Get-RequiredModules {
<#
.Synopsis
A function to get the dependencies from modules without imporing them
#>
[CmdletBinding()]
param (
# The Url to download from
[Parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("PSPath")]
$Path
)
$Definition = Get-ChildItem $Path -Filter "*.psd1"
$DefinitionData = Import-LocalizedData -BaseDirectory $Definition.Directory -FileName $Definition.Name
$DefinitionData.RequiredModules
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment