Public/Import-OriAzBopPsModule.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<#
.SYNOPSIS
    Install PowerShell module from online gallery.
    It returns dependency tree
.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
    It returns dependency tree
 
.PARAMETER DevOpsAccount
    The name of the dev ops account. Default value is 'oriflame'
 
.PARAMETER RegisterPsRepoFeedList
    Powershell Repository feed to register if needed
     
.PARAMETER RegisterNugetRepoFeedList
    Nuget Repository feed to register if needed
 
.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 Repository
    Repository feed to register if needed for getting powershell modules.
     
#####
 
.PARAMETER Guid
    Exact GUID of module.
 
.PARAMETER MaximumVersion
    Maximum module version
 
.PARAMETER RequiredVersion
    Exact required module version. When is not set the lastet version will be installed and loaded.
     
.PARAMETER Version
    Most likely minimum module versionloaded.
     
.PARAMETER Credential
    Repository Credential if needed
 
.PARAMETER SkipImport
    When is set Import-Module will be skipped
 
.PARAMETER RegisterViaPSRepository
    When is set register via Register-PSRepository, otherwise use Register-PackageSource.
 
.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
$password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force
$RepositoryCredential = New-Object System.Management.Automation.PSCredential 'feafeafae@mydomain.net',$password
 
Import-OriAzBopPsModule `
-Name OriAzEncEnvironmentConfiguration `
-RequiredVersion 1.0.48 `
-Credential $RepositoryCredential
#>

function Import-OriAzBopPsModule {
    [CmdLetBinding()]
    [OutputType([PSCustomObject])]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "The name of the dev ops account")]
        [String] $DevOpsAccount = $Script:VstsAccount,

        [Parameter(Mandatory = $true, HelpMessage = "Exact name of the module")]
        [String] $Name,

        [Parameter(Mandatory = $false, HelpMessage = "Powershell Repository feed to register if needed")]
        [String[]] $RegisterPsRepoFeedList = @('PackageManagementFeed'),

        [Parameter(Mandatory = $false, HelpMessage = "Nuget Repository feed to register if needed")]
        [String[]] $RegisterNugetRepoFeedList = @('DeploymentPackages'),

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

        [Parameter(Mandatory = $false, HelpMessage = "GUID of the module")]
        [string] $Guid,

        [Parameter(Mandatory = $false, HelpMessage = "Maximum module version")]
        [Version] $MaximumVersion,

        [Parameter(Mandatory = $false, HelpMessage = "Required module version")]
        [Version] $RequiredVersion,

        [Alias("Version")]
        [Parameter(Mandatory = $false, HelpMessage = "Most likely minimum module version")]
        [Version] $MinimumVersion,

        [Parameter(Mandatory = $false, HelpMessage = "Repository Credential if needed")]
        [PSCredential] $Credential = $null,

        [Parameter(Mandatory = $false, HelpMessage = "When is set import-module will be skipped.")]
        [switch] $SkipImport,

        [Parameter(Mandatory = $false, HelpMessage = "When is set register via Register-PSRepository, otherwise use Register-PackageSource.")]
        [switch] $RegisterViaPSRepository,

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

        [Parameter(Mandatory = $False, HelpMessage = "Max retry")]
        [int] $MaxRetry = 20
    )
    $ErrorActionPreference = 'Stop'
    Write-Verbose "-- Import-OriAzBopPsModule --"
    Write-Verbose "Name: $Name"
    Write-Verbose "Guid: $Guid"
    Write-Verbose "MaximumVersion: $MaximumVersion"
    Write-Verbose "RequiredVersion: $RequiredVersion"
    Write-Verbose "Credential: $(ConvertTo-Json $Credential)"
    Write-Verbose "SkipImport: $($SkipImport.IsPresent)"
    Write-Verbose "SleepInSec: $SleepInSec"
    Write-Verbose "MaxRetry: $MaxRetry"

    # Required module is already imported
    if (-not($SkipImport.IsPresent) -and (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 6> $null

    
    Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3 -SkipImport:$SkipImport -AllowClobber -SkipPublisherCheck
    Install-OriAzBopPrerequisity -Name PackageManagement -MinimumVersion 1.4.6 -SkipImport:$SkipImport -AllowClobber -SkipPublisherCheck

    # Required module needs to be installed and imported
    Register-OriAzBopRepository `
        -DevOpsAccount $DevOpsAccount `
        -RepositoryCredential $Credential `
        -PsProjectName $RegisterPsRepoFeedList `
        -NugetProjectName $RegisterNugetRepoFeedList `
        -RegisterViaPSRepository:$RegisterViaPSRepository `
        -SkipPrompt:$true `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference

    [PSCustomObject] $DependencyMap = Invoke-ModuleInstall `
        -Name $Name `
        -Guid $Guid `
        -MaximumVersion $MaximumVersion `
        -RequiredVersion $RequiredVersion `
        -Version $MinimumVersion `
        -Credential $Credential `
        -Repository $Repository `
        -SkipImport:$SkipImport `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference
        
    Write-Verbose "Re-Import Module InstalledLocation: $($DependencyMap.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.
    if ($SkipImport.IsPresent) {
        Write-Debug "Skip of Import-Module"
    }
    else {
        Wait-OriAzBopModuleComplete `
            -Path $DependencyMap.InstalledLocation `
            -SleepInSec $SleepInSec `
            -MaxRetry $MaxRetry `
            -Verbose:$VerbosePreference `
            -Debug:$DebugPreference

        # remove -Force while import-module (it can be already loaded via depnedency on the same module.)
        Import-Module $DependencyMap.InstalledLocation `
            -Verbose:$VerbosePreference `
            -Debug:$DebugPreference 
    }

    Write-Verbose "-- End of Import-OriAzBopPsModule --"
    return $DependencyMap
}