Public/Install-OriAzBopPrerequisity.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<#
.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 Guid
    GUID of the mmodule
 
.PARAMETER MaximumVersion
    Maximum module version
 
.PARAMETER RequiredVersion
    Required module version
 
.PARAMETER Version
    Most likely minimum module version
 
.PARAMETER PreRelease
    Name of PreRelease
     
.PARAMETER SkipImport
    When is set Import-Module will be skipped
 
.PARAMETER AllowClobber
    When is set AllowClobber will be used
 
.PARAMETER SkipPublisherCheck
    When is set SkipPublisherCheck will be used
 
.PARAMETER SkipPublisherCheck
    When is set SkipPublisherCheck will be used
 
.PARAMETER AllowPrerelease
    When is set AllowPrerelease will be used
     
.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 = "GUID of the mmodule")]
        [string] $Guid,
    
        [Parameter(Mandatory = $false, HelpMessage = "Maximum module version")]
        [Version] $MaximumVersion,
    
        [Parameter(Mandatory = $false, HelpMessage = "Required module version")]
        [Version] $RequiredVersion,
    
        [Parameter(Mandatory = $false, HelpMessage = "Minimum module version")]
        [Alias('Version')]
        [Version] $MinimumVersion,

        [Parameter(Mandatory = $false, HelpMessage = "Name of PreRelease")]
        [String] $PreRelease,        

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

        [Parameter(Mandatory = $false, HelpMessage = "When is set AllowClobber will be used.")]
        [switch] $AllowClobber,
        
        [Parameter(Mandatory = $false, HelpMessage = "When is set SkipPublisherCheck will be used.")]
        [switch] $SkipPublisherCheck,
        
        [Parameter(Mandatory = $false, HelpMessage = "When is set AllowPrerelease will be used.")]
        [switch] $AllowPrerelease
    )
    $ErrorActionPreference = 'Stop'
    Write-Verbose "-- Install-OriAzBopPrerequisity --"
    Write-Verbose "Name: $Name"
    Write-Verbose "Guid: $Guid"
    Write-Verbose "MaximumVersion: $MaximumVersion"
    Write-Verbose "RequiredVersion: $RequiredVersion"
    Write-Verbose "MinimumVersion: $MinimumVersion"
    Write-Verbose "PreRelease: $PreRelease"
    if($SkipImport.IsPresent) {
        Write-Verbose "SkipImport: true"
    } else {
        Write-Verbose "SkipImport: false"
    }
    if($AllowClobber.IsPresent) {
        Write-Verbose "AllowClobber: true"    
    } else {
        Write-Verbose "AllowClobber: false"           
    }
    if($SkipPublisherCheck.IsPresent) {
        Write-Verbose "SkipPublisherCheck: true"    
    } else {
        Write-Verbose "SkipPublisherCheck: false"           
    }
    if($AllowPrerelease.IsPresent) {
        Write-Verbose "AllowPrerelease: true"    
    } else {
        Write-Verbose "AllowPrerelease: false"           
    }
    $InstallParam = @{ 
        Name = $Name 
    }

    $Installed = Get-Module -Name $Name -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
    $installRequired = $false
    if([string]::IsNullOrEmpty($Installed)) {
        Write-Verbose "The module $Name is not installed."
        $installRequired = $true
        if(![string]::IsNullOrEmpty($MaximumVersion)) {
            $InstallParam += @{ MaximumVersion = $MaximumVersion }
        }
        if(![string]::IsNullOrEmpty($MinimumVersion)) {
            $InstallParam += @{ MinimumVersion = $MinimumVersion }
        }

        $ImportParam = $InstallParam.Clone()
        if(![string]::IsNullOrEmpty($RequiredVersion)) {
            if([string]::IsNullOrEmpty($PreRelease)) {
                $InstallParam += @{ RequiredVersion = $RequiredVersion}
                $ImportParam += @{ RequiredVersion = $RequiredVersion}
            } else {
                $InstallParam += @{ RequiredVersion = '{0}-{1}' -f $RequiredVersion, $PreRelease }
                $ImportParam += @{ RequiredVersion = $RequiredVersion}
            }
        }

    } elseif(![string]::IsNullOrEmpty($Installed.Version)) {

        [Version] $InstalledVersion = $Installed.Version
        $ImportParam = $InstallParam.Clone()

        if(![string]::IsNullOrEmpty($RequiredVersion) `
        -and (
            -not($InstalledVersion -eq $RequiredVersion) `
            -or ($InstalledVersion -eq $RequiredVersion `
                -and ![string]::IsNullOrEmpty($Installed.PrivateData) `
                -and ![string]::IsNullOrEmpty($Installed.PrivateData.PSData) `
                -and ![string]::IsNullOrEmpty($Installed.PrivateData.PSData.Prerelease) `
                -and ![string]::IsNullOrEmpty($PreRelease) `
                -and $PreRelease -ne $Installed.PrivateData.PSData.Prerelease) `
             ) `
        ) {
            Write-Verbose "The module $Name is installed in version [$InstalledVersion] PreRelase[$($Installed.PreRelease)], but required version is [$RequiredVersion] PreRelease[$PreRelease]."
            $installRequired = $true
        }

        if([string]::IsNullOrEmpty($PreRelease)) {
            $InstallParam += @{ RequiredVersion = $RequiredVersion}
            $ImportParam += @{ RequiredVersion = $RequiredVersion}
        } else {
            $InstallParam += @{ RequiredVersion = '{0}-{1}' -f $RequiredVersion, $PreRelease }
            $ImportParam += @{ RequiredVersion = $RequiredVersion}
        }

        if(![string]::IsNullOrEmpty($MaximumVersion) -and $InstalledVersion -gt $MaximumVersion) {
            Write-Verbose "The module $Name is installed in version $InstalledVersion, but maximal version is $MaximumVersion."
            $installRequired = $true
            $InstallParam += @{ MaximumVersion = $MaximumVersion }
            $ImportParam += @{ MaximumVersion = $MaximumVersion }
        } 

        if(![string]::IsNullOrEmpty($MinimumVersion) -and $InstalledVersion -lt $MinimumVersion) {
            Write-Verbose "The module $Name is installed in version $InstalledVersion, but minimal version is $MinimumVersion."
            $installRequired = $true
            $InstallParam += @{ MinimumVersion = $MinimumVersion }
            $ImportParam += @{ MinimumVersion = $MinimumVersion }
        } 
    }

    #$ImportParam = $InstallParam.Clone()

    # 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($SkipPublisherCheck.IsPresent) {
        $InstallParam += @{ 
            SkipPublisherCheck = $True 
        }
    }
    if($AllowClobber.IsPresent) {
        $InstallParam += @{ 
            AllowClobber = $True 
        }
    }
    if($AllowPrerelease.IsPresent) {
        $InstallParam += @{ 
            AllowPrerelease = $True 
        }
    }

    if($installRequired) {
        Invoke-OriAzExrExceptionRetry `
        -ListedExceptions @('Access to the path*is denied.') `
        -ScriptBlockToRun {
            Install-Module `
                @InstallParam `
                -Repository PSGallery `
                -Force `
                -Confirm:$false `
                -Verbose:$VerbosePreference `
                -Debug:$DebugPreference 
        }
    }
    
    if($SkipImport.IsPresent) {
        Write-Verbose "Skip of Import-Module"
    } else {
        Import-Module @ImportParam `
        -ErrorAction SilentlyContinue `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference 
    }

    
    Write-Verbose "-- End of Install-OriAzBopPrerequisity --"
}