Skip to content

Instantly share code, notes, and snippets.

@cinnamondev
Last active May 29, 2023 03:28
Show Gist options
  • Save cinnamondev/0997cbf17a54b086cecc507280fdd2da to your computer and use it in GitHub Desktop.
Save cinnamondev/0997cbf17a54b086cecc507280fdd2da to your computer and use it in GitHub Desktop.
Fancy-Fetch is a powershell script for some fancy fetching of modules. (install local & fetch local). Usage: Fancy-Fetch foo,bar,baz -InstallDir "./blah" -Provider "PSGallery"
function Fancy-Fetch {
param (
[string[]]$Modules,
[string]$InstallDir = './.ps-modules/',
[string]$Provider = 'PSGallery'
)
if (!(Test-Path $InstallDir)) {New-Item $InstallDir -ItemType Directory}
$Modules | Foreach-Object {
# Pull powershell-yaml if not installed
$mod_installed = (Get-Module -ListAvailable -Name $_)
$loc_installed = (Test-Path -Path "${InstallDir}/${_}")
if (!$mod_installed -and !$loc_installed) {
if ($PSModule = Find-Module -Name $_ -Repository $Provider) {
Save-Module $_ -Path $InstallDir
} else {
throw "${_} was not avilable through ${Provider}, nor in ${InstallDir} ."
}
}
if ($mod_installed) {
Import-Module $_
} else {
Import-Module -FullyQualifiedName "${InstallDir}/${_}"
}
}
}
Export-ModuleMember -Function Fancy-Fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment