Private/Find-ModuleForInstall.ps1
<#
.SYNOPSIS Try Find-module and return name of reposiory where is the module available. .PARAMETER Name Exact name of the module .PARAMETER Guid GUID of the mmodule .PARAMETER MaximumVersion Maximum module version .PARAMETER RequiredVersion Required module version .PARAMETER Version Most likely minimum module version .PARAMETER Repository Name of the repository #> function Find-ModuleForInstall { [CmdLetBinding()] [OutputType([string])] 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 = "Most likely minimum module version")] [Version] $Version, [Parameter(Mandatory = $false, HelpMessage = "Repository Credential")] [PSCredential] $Credential = $null, [Parameter(Mandatory = $false, HelpMessage = "Repository name")] [string] $Repository = $null ) $ErrorActionPreference = 'Stop' Write-Verbose '-- begin - Find-ModuleForInstall --' Write-Verbose "Name: $Name" Write-Verbose "Guid: $Guid" Write-Verbose "MaximumVersion: $MaximumVersion" Write-Verbose "RequiredVersion: $RequiredVersion" Write-Verbose "Version: $Version" [hashtable] $FindModuleParam = @{ Name = $Name } if (![string]::IsNullOrEmpty($Guid)) { $FindModuleParam += @{Guid = $Guid } } if (![string]::IsNullOrEmpty($MaximumVersion)) { $FindModuleParam += @{MaximumVersion = $MaximumVersion } } if (![string]::IsNullOrEmpty($RequiredVersion)) { # Only when is set RequiredVersion we do need add Prefix into Import-Module paramters $FindModuleParam += @{RequiredVersion = $RequiredVersion } } if (![string]::IsNullOrEmpty($Version)) { $FindModuleParam += @{Version = $Version } } if ($null -ne $Repository) { $FindModuleParam += @{Repository = $Repository } } if ($null -ne $Credential) { $FindModuleParam += @{Credential = $Credential } } Write-Verbose "Find-Module param" Write-Verbose (ConvertTo-Json $FindModuleParam) $toReturn = $null $moduleInRepo = Find-Module ` @FindModuleParam ` -Verbose:$VerbosePreference ` -Debug:$DebugPreference Write-Verbose "Result of find-module begin:" Write-Verbose (ConvertTo-Json $moduleInRepo) Write-Verbose "Result of find-module end" if ([String]::IsNullOrEmpty($moduleInRepo)) { Write-Error "Required Module [$Name] not found in any repo." } Write-Verbose "Return of Find-ModuleForInstall: $toReturn" Write-Verbose '-- end - Find-ModuleForInstall --' return $toReturn } |