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 SkipImport When is set Import-Module will be skipped .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 = "When is set import-module will be skipped.")] [switch] $SkipImport ) $ErrorActionPreference = 'Stop' Write-Debug "-- Install-OriAzBopPrerequisity --" Write-Debug "Name: $Name" Write-Debug "Guid: $Guid" Write-Debug "MaximumVersion: $MaximumVersion" Write-Debug "RequiredVersion: $RequiredVersion" Write-Debug "MinimumVersion: $MinimumVersion" $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($RequiredVersion)) { $InstallParam += @{ RequiredVersion = $RequiredVersion } } if(![string]::IsNullOrEmpty($MaximumVersion)) { $InstallParam += @{ MaximumVersion = $MaximumVersion } } if(![string]::IsNullOrEmpty($MinimumVersion)) { $InstallParam += @{ MinimumVersion = $MinimumVersion } } } elseif(![string]::IsNullOrEmpty($Installed.Version)) { if(![string]::IsNullOrEmpty($RequiredVersion) -and $Installed.Version -ne $RequiredVersion) { Write-Verbose "The module $Name is installed in version $($Installed.Version), but required version is $RequiredVersion." $installRequired = $true $InstallParam += @{ RequiredVersion = $RequiredVersion } } if(![string]::IsNullOrEmpty($MaximumVersion) -and $Installed.Version -gt $MaximumVersion) { Write-Verbose "The module $Name is installed in version $($Installed.Version), but maximal version is $MaximumVersion." $installRequired = $true $InstallParam += @{ MaximumVersion = $MaximumVersion } } if(![string]::IsNullOrEmpty($MinimumVersion) -and $Installed.Version -lt $MinimumVersion) { Write-Verbose "The module $Name is installed in version $($Installed.Version), but minimal version is $MinimumVersion." $installRequired = $true $InstallParam += @{ 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 $InstallParam += @{ SkipPublisherCheck = $True AllowClobber = $True } if($installRequired) { Invoke-OriAzExrExceptionRetry ` -ListedExceptions @('Access to the path*is denied.') ` -ScriptBlockToRun { Install-Module ` @InstallParam ` -Repository PSGallery ` -Verbose:$VerbosePreference ` -Debug:$DebugPreference ` -Force } } if($SkipImport.IsPresent) { Write-Debug "Skip of Import-Module" } else { Import-Module @ImportParam ` -ErrorAction SilentlyContinue ` -Verbose:$VerbosePreference } Write-Debug "-- End of Install-OriAzBopPrerequisity --" } |