Public/Get-specFileHashAndDownload.ps1

function Get-specFileHashAndDownload {
    <#
    .SYNOPSIS
        Obtains the file hash for a local file and compares it with the hash of a remote file. Downloads the remote file if hashes differ or the local file does not exist.
 
    .DESCRIPTION
        This helper function obtains the hash of a local file and the hash of a remote file, compares them, and takes appropriate actions based on the comparison.
 
    .PARAMETER deviceScriptLocationOnDevice
        The local path of the file on the device.
 
    .PARAMETER deviceSourceURL
        The URL of the remote file.
 
    .EXAMPLE
        Get-specFileHashAndDownload -ScriptLocationOnDevice "C:\Path\ToLocal\File.ps1" -SourceURL "https://example.com/Remote/File.ps1"
 
    .NOTES
        Author: owen.heaume
        Version: 1.0
    #>

    param (
        [string]$deviceScriptLocationOnDevice,
        [string]$deviceSourceURL
    )

    Write-Host "Obtaining local file hash: '$deviceScriptLocationOnDevice'...." -ForegroundColor DarkCyan -NoNewline

    #1. Get File hashes of the local file which will be used to compare against remote file. (Assign NULL if local file does not exist)
    If (Test-Path $deviceScriptLocationOnDevice) {
        $LocalHash = Get-SpecMDFileHashBase64 -FilePath $deviceScriptLocationOnDevice
        Write-Host 'OK' -ForegroundColor DarkGreen
    } else {
        $LocalHash = $null
        Write-Host 'File does not exist on this device' -ForegroundColor DarkGray
    }

    #2. Get the hash of the remote file
    Write-Host 'Obtaining the remote file hash....' -ForegroundColor DarkCyan -NoNewline
    try {
        $RemoteHash = Get-SpecMDFileHashBase64 -FileURL $deviceSourceURL -ErrorAction Stop
        Write-Host 'OK' -ForegroundColor DarkGreen
    } catch {
        Write-Host 'Unable to get the remote file hash - check the URL is correctly formed' -ForegroundColor DarkYellow
    }

    #2b. Display the hashes
    Write-Host 'Comparing hash values....' -ForegroundColor DarkCyan
    Write-Host " Local hash: $LocalHash" -ForegroundColor DarkGray
    Write-Host "Remote hash: $RemoteHash" -ForegroundColor DarkGray

    #3. If hashes match - then nothing to do
    if ($LocalHash -eq $RemoteHash) {
        Write-Host "The hashes match - the file stored on the device is the latest version`n" -ForegroundColor DarkGreen
    }

    #4. Hashes do not match, or the local file does not exist - download the file from blob storage.
    if ($LocalHash -ne $RemoteHash) {
        Write-Host 'Local file hash is different or null compared to remote file, so downloading remote file' -ForegroundColor DarkGray
        # Download file to designated path on the local device (overwrites if already there)
        $result = Get-SpecBlobFileFromURL -FileUrl $deviceSourceURL -DestinationFilePath $deviceScriptLocationOnDevice
        if ($null -eq $result) {
            Write-Host "Downloaded file OK`n" -ForegroundColor DarkGreen
        } else {
            Write-Host 'An error occurred downloading the file' -ForegroundColor DarkYellow
        }
    }
}