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-Debug '-- begin - Find-ModuleForInstall --'
    Write-Debug "Name: $Name"
    Write-Debug "Guid: $Guid"
    Write-Debug "MaximumVersion: $MaximumVersion"
    Write-Debug "RequiredVersion: $RequiredVersion"
    Write-Debug "Version: $Version"

    $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 $Credential) {
        $FindModuleParam += @{Credential = $Credential}
    }
    if($null -ne $Repository) {
        $FindModuleParam += @{Repository = $Repository}
    }
    
    Write-Verbose "Find-Module param"
    Write-Verbose (ConvertTo-Json $FindModuleParam)
    
    $toReturn = $null
    try {
        $moduleInRepo = Find-Module `
                        @FindModuleParam `
                        -Verbose:$VerbosePreference
    
        Write-Verbose "Result of find-module begin:"
        Write-Verbose (ConvertTo-Json $moduleInRepo)
        Write-Verbose "Result of find-module end"
    
        #When is registred more repo with the content only one should be provided.
        $toReturn = ($moduleInRepo | Select-Object -First 1)
    }
    catch {
        $formatstring = "{0} : {1}`n{2}`n" +
        " + CategoryInfo : {3}`n" +
        " + FullyQualifiedErrorId : {4}`n"
        $fields =   $_.InvocationInfo.MyCommand.Name,
                    $_.ErrorDetails.Message,
                    $_.InvocationInfo.PositionMessage,
                    $_.CategoryInfo.ToString(),
                    $_.FullyQualifiedErrorId

        Write-Warning $formatstring -f $fields
    }
    
    if ([String]::IsNullOrEmpty($moduleInRepo)) {
        Write-Error "Required Module [$Name] not found in any repo."
    }
    Write-Verbose "Return of Find-ModuleForInstall: $toReturn"
    Write-Debug '-- end - Find-ModuleForInstall --'
    return $toReturn
}