Private/Find-ModuleForInstall.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<#
.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 (![string]::IsNullOrEmpty($Repository)) {
        $FindModuleParam += @{Repository = $Repository }
    }
    if ($null -ne $Credential) {
        $FindModuleParam += @{Credential = $Credential }
    }
    
    Write-Verbose "Find-Module param"
    Write-Verbose (ConvertTo-Json $FindModuleParam)
    
    $toReturn = $null

    $toReturn = Find-Module `
        @FindModuleParam `
        -Verbose:$VerbosePreference `
        -Debug:$DebugPreference 
    
    Write-Verbose "Result of find-module begin:"
    if([string]::IsNullOrEmpty($toReturn)) {
        throw "The module not found. Search criteria [$(ConvertTo-Json $FindModuleParam)]"
    } else {
        Write-Verbose (ConvertTo-Json $toReturn)
    }
    Write-Verbose "Result of find-module end"
    
    Write-Verbose '-- End of Find-ModuleForInstall --'
    return $toReturn
}