Public/Get-OriAzBopFolderHash.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
<#
.SYNOPSIS
    Compute Hash of the folder
 
.DESCRIPTION
    Compute Hash of the folder
 
.PARAMETER Path
    The Path to folder to compute the Hash
 
.PARAMETER ExcludePath
    Exclude Path is path to the Manifest file (.PSD1 suffix)
 
 
.EXAMPLE
Get-OriAzBopFolderHash `
-Path c:\temp\
 
# Should return something like this:
# 5cfdcbf97b7a004f387c69d6b1dab25b
#>


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

        [Parameter(Mandatory = $False, HelpMessage = "Exclude Path is path to the Manifest file (.PSD1 suffix)")]
        [String[]] $ExcludePath
    )
    $ErrorActionPreference = 'Stop'
    Write-Verbose "-- Get-OriAzBopFolderHash --"
    Write-Verbose "Path: $Path"
    Write-Verbose "ExcludePath: $ExcludePath"
    $ExcludePath += "*.tests.ps1"
    $ExcludePath += ".*" # There can be e.g. files like .gitignore
    $ExcludePath += "PSGetModuleInfo.xml"

    

    $ToReturn = Invoke-OriAzExrExceptionRetry `
        -ListedExceptions @('Access to the path*is denied.', 'Administrator rights are required to install or update.', 'The process cannot access the file') `
        -ScriptBlockToRun `
    {
        [CmdletBinding()]
        param
        (   
            [Parameter(Mandatory = $true, HelpMessage = "Path to the module")]
            [string] $Path,
                
            [Parameter(Mandatory = $true, HelpMessage = "Exclude path")]
            [string] $ExcludePath
        )
        $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
        [string] $ToReturn = [System.BitConverter]::ToString($md5.ComputeHash([System.Text.Encoding]::UTF8.GetBytes((
                        Get-ChildItem -Path $Path -Recurse -Exclude $ExcludePath -File `
                        | Where-Object { (Split-Path $_.Directory -leaf) -ne 'Obsolete' } `
                        | Where-Object { (Split-Path $_.Directory -leaf) -ne 'Tests' } `
                        | Get-Content  -Raw ))))
        return $ToReturn
    } `
        -MaxRetry 30 `
        -SleepTimeInSec 10


    # CleanUp of separator and make it all upper cases
    $ToReturn = $ToReturn.ToUpper().Replace('-', '')

    <#
    $HashCollector = Get-ChildItem -Path $Path -Recurse -Exclude $ExcludePath -File `
    | Where-Object { (Split-Path $_.Directory -leaf) -ne 'Obsolete' } `
    | Where-Object { (Split-Path $_.Directory -leaf) -ne 'Tests' } `
    | Get-FileHash -Algorithm MD5
 
    $allBytes = new-object System.Collections.Generic.List[byte]
    foreach ($OneHashCollector in $HashCollector) {
        $allBytes.AddRange([System.Text.Encoding]::UTF8.GetBytes($OneHashCollector.Hash))
        $allBytes.AddRange([System.IO.File]::ReadAllBytes($file.FullName))
        $allBytes.AddRange([System.Text.Encoding]::UTF8.GetBytes($file.Name))
    }
    $hasher = [System.Security.Cryptography.MD5]::Create()
    $ToReturn = [string]::Join("", $($hasher.ComputeHash($allBytes.ToArray()) | ForEach-Object { "{0:x2}" -f $_ }))
 
     
    $ToReturn = $HashCollector.Hash
    #>

    Write-Verbose "hash of $Path is [$ToReturn]."
    Write-Verbose "-- End of Get-OriAzBopFolderHash --"
    return $ToReturn

}