Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active December 28, 2017 14:26
Show Gist options
  • Save turboBasic/51eaf2960a1b296d6baa4b7bd2d4670c to your computer and use it in GitHub Desktop.
Save turboBasic/51eaf2960a1b296d6baa4b7bd2d4670c to your computer and use it in GitHub Desktop.
[Resolve-Module.ps1] Imports and upgrades module dependencies #powershell
# by Brandon Padgett
function Resolve-Module
{
[Cmdletbinding()]
Param
(
[Parameter(Mandatory)]
[string[]] $Name
)
Process
{
foreach ($ModuleName in $Name)
{
$Module = Get-Module -Name $ModuleName -ListAvailable
Write-Verbose -Message "Resolving Module $($ModuleName)"
if ($Module)
{
$Version = $Module | Measure-Object -Property Version -Maximum | Select-Object -ExpandProperty Maximum
$GalleryVersion = Find-Module -Name $ModuleName -Repository PSGallery | Measure-Object -Property Version -Maximum | Select-Object -ExpandProperty Maximum
if ($Version -lt $GalleryVersion)
{
if ((Get-PSRepository -Name PSGallery).InstallationPolicy -ne 'Trusted') { Set-PSRepository -Name PSGallery -InstallationPolicy Trusted }
Write-Verbose -Message "$($ModuleName) Installed Version [$($Version.tostring())] is outdated. Installing Gallery Version [$($GalleryVersion.tostring())]"
Install-Module -Name $ModuleName -Force
Import-Module -Name $ModuleName -Force -RequiredVersion $GalleryVersion
}
else
{
Write-Verbose -Message "Module Installed, Importing $($ModuleName)"
Import-Module -Name $ModuleName -Force -RequiredVersion $Version
}
}
else
{
Write-Verbose -Message "$($ModuleName) Missing, installing Module"
Install-Module -Name $ModuleName -Force
Import-Module -Name $ModuleName -Force -RequiredVersion $Version
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment