Public/Install-OriAzBopPrerequisity.ps1

<#
.SYNOPSIS
    Install modules required from PSGallery
.DESCRIPTION
    Install modules required from PSGallery
 
.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 RequiredVersion
    Specifies the exact version of a single module to install.
.EXAMPLE
    Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3
#>

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

        [Parameter(Mandatory = $false, HelpMessage = "Mininal version")]
        [Version] $MinimumVersion
    )
    $ErrorActionPreference = 'Stop'
    Write-Debug "-- Install-OriAzBopPrerequisity --"
    Write-Debug "Name: $Name"
    Write-Debug "MinimumVersion: $MinimumVersion"

    $Installed = Get-Module -Name $Name
    if([string]::IsNullOrEmpty($Installed) `
    -or (![string]::IsNullOrEmpty($Installed) `
        -and ![string]::IsNullOrEmpty($Installed.Version) `
        -and ![string]::IsNullOrEmpty($MinimumVersion) `
        -and $Installed.Version -lt $MinimumVersion)) {
        Write-Verbose "Updating $Name module to the $MinimumVersion version.";

        $InstallParam = @{ 
            Name = $Name 
        }
        # The SkipPublisherCheck has been added due to new version of PowerShellGet 2.2.4 with dependency on PackageManagement 1.4.6
        # The PackageManagement 1.4.6 is quite every where and has been already required while instalation of PowerShellGet 2.2.3
        # However while the installation process is the PackageManagement 1.4.6 already loaded
        # and is not possible to override PackageManagement (it is cycle)
        # The solution is the SkipPublisherCheck flag
        if($Name -eq "PowerShellGet") {
            $InstallParam += @{ SkipPublisherCheck = $True }
        }


        if(![string]::IsNullOrEmpty($MinimumVersion)) {
            $InstallParam += @{ MinimumVersion = $MinimumVersion }
        }
        Invoke-OriAzExrExceptionRetry `
        -ListedExceptions @('Access to the path*is denied.') `
        -ScriptBlockToRun {
            Install-Module `
                @InstallParam `
                -Repository PSGallery `
                -Verbose:$VerbosePreference `
                -Debug:$DebugPreference `
                -Force 
        }
        
        Import-Module $Name `
        -MinimumVersion $MinimumVersion `
        -ErrorAction SilentlyContinue `
        -Verbose:$VerbosePreference 
    }
    Write-Debug "-- End of Install-OriAzBopPrerequisity --"
}