Public/Import-OriAzBopPsModule.ps1

<#
.SYNOPSIS
    Install PowerShell module from online gallery.
.DESCRIPTION
    This cmdlet will try to install PowerShell module from online gallery.
    It does the following:
    1. Checks if required module is already loaded with a required (or higher) version.
    2. If not, tryes to import required version from installed modules.
    3. If the loaded module version is still lower or missing (it is not installed) it tries to install it
    4. Checks for prerequisities like PS version, PackageManager version etc
    5. Installs necessary tools if needed
    6. Registers repository if not yet
    7. Installs the module
    8. Loads required version of the module
 
.PARAMETER Name
    Specifies the exact names of modules to install from the online gallery. The module name must match the module name in the repository.
     
.PARAMETER RepoOriFeedName
    Repository feed to register if needed for getting powershell modules.
     
.PARAMETER RequiredVersion
    Exact required module version. When is not set the lastet version will be installed and loaded.
     
.PARAMETER Credential
    Repository Credential if needed
 
.EXAMPLE
$password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
$RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net',$password
 
Import-OriAzBopPsModule `
-Name OriAzEncEnvironmentConfiguration `
-RequiredVersion 1.0.48 `
-Credential $RepoCredential
#>

function Import-OriAzBopPsModule {
[CmdLetBinding()]
param (
    [Parameter(Mandatory = $true, HelpMessage = "Exact name of the module")]
    [String] $Name,

    [Parameter(Mandatory = $false, HelpMessage = "Repository feed to register if needed for getting powershell modules")]
    [String] $RepoOriFeedName = 'PackageManagementFeed',

    [Parameter(Mandatory = $false, HelpMessage = "Exact required module version. When is not set the lastet version will be installed and loaded.")]
    [Version] $RequiredVersion,

    [Parameter(Mandatory=$false, HelpMessage = "Repository Credential if needed")]
    [PSCredential] $Credential = $null
)
    $ErrorActionPreference = 'Stop'
    Write-Debug "-- Import-OriAzBopPsModule --"
    Write-Debug "Name: $Name"
    Write-Debug "RequiredVersion: $RequiredVersion"

    # Required module is already imported
    if (Test-GetModule -Name $Name -RequiredVersion $RequiredVersion) {
        Write-Verbose "Module $Name is already imported."
        return
    }

    # Fix credential provider
    Invoke-WebRequest -Uri "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" -UseBasicParsing | Invoke-Expression |Out-Null

    Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3
    Install-OriAzBopPrerequisity -Name PackageManagement -MinimumVersion 1.4.6

    # Required module needs to be installed and impoted
    Register-OriAzBopOriflameFeed `
    -feeds $RepoOriFeedName `
    -Credential $Credential `
    -Verbose:$VerbosePreference `
    -Debug:$DebugPreference

    $InstalledLocation = Invoke-ModuleInstall `
    -Name $Name `
    -RequiredVersion $RequiredVersion `
    -Credential $Credential `
    -Repository $RepoOriFeedName `
    -Verbose:$VerbosePreference `
    -Debug:$DebugPreference
        
    Write-Verbose "Re-Import Module InstalledLocation: $InstalledLocation"
    # Note: Following import does NOT work.
    # Import-Module -Name $Name -RequiredVersion $RequiredVersion -Verbose
    # Any using of -RequiredVersion skip execution of init.ps1
    # This problem we're bpassing via using Import-Module on installed path of module.
    Import-Module $InstalledLocation `
    -Verbose:$VerbosePreference `
    -Debug:$DebugPreference `
    -Force

    Write-Debug "-- End of Import-OriAzBopPsModule --"
}