Public/Get-specTonorefIcons.ps1

Function Get-specTonorefIcons {
    <#
    .SYNOPSIS
        Downloads Tonoref icons from a remote location if needed, ensuring local copies match what's available remotely.
 
    .DESCRIPTION
        This function helps manage icons for Tonoref devices. It checks local icon files against remote versions (using hash comparisons) and downloads them only if they are missing or outdated.
 
    .PARAMETER IconName
        Specifies the name (or names) of the icons to check and potentially download. Accepts pipeline input.
 
    .PARAMETER LocalIconPath
        The path on the local machine where icons should be stored.
 
    .PARAMETER RemoteIconPath
        The base URL path of the remote location where the icons are stored. Do not include the icon name or SAS token in this parameter.
 
    .PARAMETER sasReadToken
        A valid SAS Read Token for accessing the remote icon storage.
 
    .EXAMPLE
        $iconList = "Icon1.png", "Icon2.png"
        $iconList | Get-specTonorefIcons -LocalIconPath "C:\Icons\" -RemoteIconPath "https://icons.storage.blob.core.windows.net/container" -sasReadToken "?sv=2020-08-04&ss=bfqt&srt=sco&sp=rl&st=2022-10-11&se=2023-12-31&sig=0a0b34c12d..."
 
    .NOTES
        Author: owen.heaume
        Version: 1.0.0 - Initial release
                 1.0.1 - Remove redundant error handling
    #>


    [cmdletBinding()]
    [Alias ('Download-specTonorefIconsIfRequired')]

    param (
        [Parameter(Mandatory, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$IconName,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName = $true)]
        [string]$LocalIconPath,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName = $true)]
        [string]$RemoteIconPath,

        [Parameter(Mandatory, ValueFromPipelineByPropertyName = $true)]
        [string]$sasReadToken
    )

    Begin {
    }

    Process {
        foreach ($icon in $IconName) {
            Write-Host "Checking for $icon" -ForegroundColor DarkCyan
            $fullLocalIconPath = Join-Path -Path $localIconPath -ChildPath $icon
            $fullRemoteIconPath = "$remoteIconPath/$icon" + $sasReadToken

            If (Test-Path $fullLocalIconPath) {
                $LocalHash = Get-SpecMDFileHashBase64 -FilePath $fullLocalIconPath
            } else {
                $LocalHash = $null
            }

            #2. Get the hash of the remote file
            $RemoteHash = Get-SpecMDFileHashBase64 -FileURL $fullRemoteIconPath


            #3. If hashes match - then nothing to do
            if ($LocalHash -eq $RemoteHash) {
                Write-Host "Local file [$FullLocalIconPath] already exists and has the same hash as remote file`n" -ForegroundColor DarkGray
            }

            #4. Hashes do not match - 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 designate path on local device (overwrites if already there)
                $result = Get-SpecBlobFileFromURL -FileUrl $fullRemoteIconPath -FilePath $fullLocalIconPath
            }
        }
    }
}