Scripts/Import-DependentModules.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
param([Parameter(mandatory=$true)][string]$ModuleRoot)

function Get-LatestInstalledModule {
    param([string]$Name)
    Get-Module -Name $Name -ListAvailable | sort -Property Version -Descending | Select-object -First 1
}

$module = Get-Module -FullyQualifiedName $ModuleRoot -ListAvailable -ErrorAction SilentlyContinue
if($module -and $module.PrivateData -and $module.PrivateData.PSData -and $module.PrivateData.PSData.ExternalModuleDependencies) {
    $module.PrivateData.PSData.ExternalModuleDependencies | foreach {
        $depModule = Get-LatestInstalledModule $_
        if(-not $depModule) {
            Write-Host "Module dependency '$($_)' was not installed, will attempt to install from default PS Repository ..."
            try {
               # & "$ModuleRoot\Scripts\Set-DefaultPSGalleryUrl.ps1" -RemoveAllOtherRepositories -RepoUrl '[[$PrivatePowerShellRepo]]'
                Install-Module -Name $_ -Force 
                $depModule = Get-LatestInstalledModule $_           
            } catch {
                Write-Warning $_
            }

        }
        
        Write-Host "Importing dependent module '$($_)' ..."
        try {
            $depModule | Import-Module -Force  -Scope Global
        } catch {
            Write-Warning $_
        }
    }
}