Public/Wait-OriAzBopModuleComplete.ps1

<#
.SYNOPSIS
    Should return wait till the CRC of module is ok or MaxRetry is reched.
 
.DESCRIPTION
    Should return wait till the CRC of module is ok or MaxRetry is reched.
 
.PARAMETER Path
    Specifies the exact names of modules to install from the online gallery. The module name must match the module name in the repository.
 
.PARAMETER SleepInSec
    Sleep time in sec between test if the module is ready to import.
 
.PARAMETER MaxRetry
    Max retry while waiting to import module
 
 
.EXAMPLE
Wait-OriAzBopModuleComplete `
-Path c:\temp\ `
-SleepInSec 30 `
-MaxRetry 20
 
#>


function Wait-OriAzBopModuleComplete {
    [CmdLetBinding()]
    param (
        [Parameter(Mandatory = $True, HelpMessage = "The Path to folder to compute the Hash")]
        [ValidateScript( { Test-Path $_ })]
        [String] $Path,

        [Parameter(Mandatory = $False, HelpMessage = "Sleep time in sec between test if the module is ready to import.")]
        [int] $SleepInSec = 3,

        [Parameter(Mandatory = $False, HelpMessage = "Max retry")]
        [int] $MaxRetry = 20

    )
    $ErrorActionPreference = 'Stop'
    Write-Verbose "-- Wait-OriAzBopModuleComplete --"
    Write-Verbose "Path: $Path"
    Write-Verbose "SleepInSec: $SleepInSec"
    Write-Verbose "MaxRetry: $MaxRetry"

    [string] $RequiredMd5 = Get-OriAzBopModuleMd5FromTag -Path $Path -Verbose:$VerbosePreference -Debug:$DebugPreference

    if ([string]::IsNullOrEmpty($RequiredMd5)) {
        Write-Debug "No MD5 found in module."
    }
    else {
        Write-Debug "the MD5 [$RequiredMd5] found in module."
        [int] $try = 1    
        While (-not(Test-OriAzBopModuleComplete -Path $Path -RequiredMd5 $RequiredMd5 -Verbose:$VerbosePreference -Debug:$DebugPreference)) {
            Write-Debug "Retry number [$try]. Max retry is [$MaxRetry]"
            if ($try -ge $MaxRetry) {
                throw "The module [$Path] is not complete based on MD5 in the module tag is expected [$RequiredMd5]. Max retry [$MaxRetry] reached."
            }
            Start-Sleep -Seconds $SleepInSec
            $try++
        }
    }

    Write-Verbose "-- End of Wait-OriAzBopModuleComplete --"
}