Public/Get-OriAzBopModuleMd5FromTag.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
<#
.SYNOPSIS
    Get MD5 from the tag if exists
 
.DESCRIPTION
    Get MD5 from the tag if exists
 
.PARAMETER Path
    Path to the Module manifest .PSD1 file
 
.PARAMETER SeachMask
    The seach mask to get tag with MD5
 
 
.EXAMPLE
Get-OriAzBopModuleMd5FromTag `
-Path c:\temp\myModule.psd1
 
# Should return something like this when the tag MD5-5cfdcbf97b7a004f387c69d6b1dab25b is present:
# 5cfdcbf97b7a004f387c69d6b1dab25b
#>


function Get-OriAzBopModuleMd5FromTag {
    [CmdLetBinding()]
    [Outputtype("String")]
    param (
        [Parameter(Mandatory = $True, HelpMessage = "The Path to folder to compute the Hash")]
        [ValidateScript( { Test-Path $_ })]
        [String] $Path,

        [Parameter(Mandatory = $False, HelpMessage = "The seach mask to get tag with MD5")]
        [String] $SeachMask = 'MD5_v3-*'
    )
    $ErrorActionPreference = 'Stop'
    Write-Verbose "-- Get-OriAzBopModuleMd5FromTag --"
    Write-Verbose "SeachMask: $SeachMask"

    [string] $RequiredMd5 = ''
    $Manifest = Import-PowershellDataFile -LiteralPath $Path
    foreach ($onePrivateData in $Manifest.PrivateData) {
        foreach ($onePSData in $onePrivateData.PSData) {
            if ([string]::IsNullOrEmpty($onePSData.Tags)) {
                [Object[]] $Tags = @()
            }
            else {
                [Object[]] $Tags = $onePSData.Tags 
            }
            
            # Get only tags releavant to this module
            [String] $Md5Tag = $Tags | Where-Object { $_ -ilike $SeachMask } | Select-Object -First 1
            [string[]] $Md5TagmArrray = $Md5Tag -split '-', 2
            if (![string]::IsNullOrEmpty($Md5TagmArrray[0]) `
                    -and ![string]::IsNullOrEmpty($Md5TagmArrray[1])) {
                $RequiredMd5 = $Md5TagmArrray[1]
            }
        }
    }

    Write-Verbose "-- End of Get-OriAzBopModuleMd5FromTag --"
    return $RequiredMd5

}