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