Private/Get-OriAzBopVersionParamHelper.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
<#
.SYNOPSIS
    Get module into Version param structure
 
.PARAMETER Module
    Informations about required module
 
.EXAMPLE
Get-OriAzBopVersionParamHelper `
-Module @{'ModuleA',@{ModuleName='ModuleB';Guid=123;MaximumVersion='1.0.10';ModuleVersion='1.0.3';Version='1.0.2';PreRelease='pre06';AllowPrerelease='1'}}
#>

function Get-OriAzBopVersionParamHelper {
    [CmdLetBinding()]
    [OutputType([Hashtable])]
    param (        
        [Parameter(Mandatory = $true, HelpMessage = "Informations about required module")]
        $Module
    )
    $ErrorActionPreference = 'Stop'    
    Write-Verbose '-- begin - Get-OriAzBopVersionParamHelper --'
    Write-Debug "Module: $(ConvertTo-Json $Module)"
    # There can be in RequiredModules string or in ModuleVersion type
    # See possible parameters for parse
    # https://docs.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.modulespecification?view=pscore-6.2.0#properties
    [Hashtable] $toReturn = @{}
    if ('System.String' -eq $Module.getType().fullname) {
        $toReturn += @{Name = $Module }
    }
    else {
        # The type of $Module is most likely ModuleSpecification represent by HashTable at the moment.
        $toReturn += @{Name = $Module.ModuleName }
        if (![string]::IsNullOrEmpty($Module.Guid)) {
            $toReturn += @{Guid = $Module.Guid }
        }
        if (![string]::IsNullOrEmpty($Module.MaximumVersion)) {
            $toReturn += @{MaximumVersion = $Module.MaximumVersion }
        }
        if (![string]::IsNullOrEmpty($Module.ModuleVersion)) {
            # Only when is set RequiredVersion we do need add Prefix into Import-Module paramters
            $toReturn += @{RequiredVersion = $Module.ModuleVersion }
        }
        if (![string]::IsNullOrEmpty($Module.Version)) {
            $toReturn += @{Version = $Module.Version }
        }
        if (![string]::IsNullOrEmpty($Module.PreRelease)) {
            $toReturn += @{PreRelease = $Module.PreRelease }
        }
        if (![string]::IsNullOrEmpty($Module.AllowPrerelease)) {
            $toReturn += @{AllowPrerelease = $Module.AllowPrerelease }
        }
    }
    Write-Debug (ConvertTo-Json -InputObject $toReturn)
    Write-Verbose '-- End of Get-OriAzBopVersionParamHelper --'
    return $toReturn
}