Public/Get-AzBlobMD5.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
function Get-AzBlobMD5 {
    [cmdletbinding()]
    param(
        # File path, best to use the fully qualified path (C:\path\to.file) rather than relative (.\path\to.file)
        [Parameter(ValueFromPipeline=$true)]
        [string]
        $FilePath
    )

    begin {
        $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    }

    process {
        foreach ($file in $FilePath) {
            try {
                $fs = [System.IO.FileStream]::new($file,[System.IO.FileMode]::Open)
            }
            catch {
                Write-Error -Message ($error[0]).Exception.InnerException.Message
                break
            }
            $hash = [System.Convert]::ToBase64String($md5.ComputeHash($fs))
            $fs.Close()

            [pscustomobject]@{
                File = $file
                Hash = $hash
            }
        }
    }
}