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 }
        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 --"
}