Public/Wait-OriAzBopModuleComplete.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.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 --"
}