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
.PARAMETER Proxy
    Use it when is required to use proxy while accessing the repository
.PARAMETER ProxyCredential
    Use it when is required to use proxy with credential
#>

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,

        [Parameter(Mandatory = $false, HelpMessage = "Use it when is required to use proxy while accessing the repository")]
        [Uri] $Proxy = $null,
    
        [Parameter(Mandatory = $false, HelpMessage = "Use it when is required to use proxy with credential")]
        [PSCredential] $ProxyCredential = $null
    )
    $ErrorActionPreference = 'Stop'
    Write-Verbose -Message ("[ START: {0}:{1} (v.{2}) ]" -f $Local:MyInvocation.MyCommand.Source, $Local:MyInvocation.MyCommand.Name, $Local:MyInvocation.MyCommand.Version)
    foreach ($arg in $PSBoundParameters.GetEnumerator()) {
        if ([string]::IsNullOrEmpty($arg.Value)) {
            Write-Debug -Message ("[null] {0}: {1}" -f $arg.Key, $arg.Value) -ErrorAction SilentlyContinue 
        }
        else {
            Write-Debug -Message ("[{2}] {0}: {1}" -f $arg.Key, $arg.Value, $arg.Value.GetType().Name) -ErrorAction SilentlyContinue 
        }
    }

    [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 (![string]::IsNullOrEmpty($Repository)) {
        $FindModuleParam += @{Repository = $Repository }
    }
    if ($null -ne $Credential) {
        $FindModuleParam += @{Credential = $Credential }
    }
    
    if (![string]::IsNullOrEmpty($Proxy)) {
        $FindModuleParam += @{Proxy = $Proxy }
    }
    if ($null -ne $ProxyCredential) {
        $FindModuleParam += @{ProxyCredential = $ProxyCredential }
    }
    
    Write-Debug "Find-Module param"
    Write-Debug (ConvertTo-Json $FindModuleParam)
    
    $toReturn = $null

    $toReturn = Find-Module `
        @FindModuleParam `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference 
    
    #Write-Debug "Result of find-module begin:"
    if ([string]::IsNullOrEmpty($toReturn)) {
        throw "The module not found. Search criteria [$(ConvertTo-Json $FindModuleParam)]"
    }
    else {
        ; #Write-Debug (ConvertTo-Json $toReturn)
    }
    #Write-Debug "Result of find-module end"
    
    Write-Verbose -Message ("[ END: {0} ]" -f $Local:MyInvocation.MyCommand.Name)
    return $toReturn
}