Functions/Install-XlsConverterProgram.ps1

<#
.SYNOPSIS
    This function installs the XLS converter program in the specified location.
#>

function Install-XlsConverterProgram {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([Bool])]
    param (
        # The path to the folder where the program will be installed.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$installFolderPath
    )

    # Generate the program zip file path
    $zipFilePath = "$($installFolderPath)\XlsConverter.zip"

    # Verify the filepath
    if (!(Test-Path -Path $installFolderPath -ErrorAction SilentlyContinue)) {
        Write-Error "The path '$($installFolderPath)' is not present."
        return $false
    }

    # Download the program zip file
    Write-Information "Downloading the XLS converter program."
    if (!(Get-FileFromUrl -FileUrl "https://bittitangdpreport.blob.core.windows.net/template/XlsConverter.zip" -DestinationPath $zipFilePath)) {
        Write-Error "Failed to download the XLS converter program."
        return $false
    }

    # Unzip the program zip file
    Write-Information "Unzipping the XLS converter program."
    try {
        Expand-Archive -Path $zipFilePath -DestinationPath $installFolderPath -Force -Confirm:$false
    }
    catch {
        Write-Error "Exception occurred while unzipping the XLS converter program.`r`n$($_.Exception.Message)"
        return $false
    }

    # Test for all the files in the zip
    Write-Information "Verifying the XLS converter program files."
    $expectedFiles = @(
        @{
            Name = "Microsoft.mshtml.dll"
            Hash = "BB9DFD6A5699C9318918F3971277A228"
        },
        @{
            Name = "Spire.License.dll"
            Hash = "F0F4A62AB7ADE480D7B89DE488222DAB"
        },
        @{
            Name = "Spire.Pdf.dll"
            Hash = "A5743F20B8753CFBC965BF54D7B07660"
        },
        @{
            Name = "Spire.Xls.dll"
            Hash = "2EFFE494B006B61FD2D8A93D1D660671"
        },
        @{
            Name = "license.elic.xml"
            Hash = "7881E16E3B4FED8B44A0FCDC391A326A"
        }
    )
    foreach ($expectedFile in $expectedFiles) {
        $fullFilePath = Join-Path $installFolderPath $expectedFile.Name

        # Ensure that file is present
        if (!(Test-Path -Path $fullFilePath -ErrorAction SilentlyContinue)) {
            Write-Error "Expected file '$($expectedFile.Name)' to be present after unzipping the XLS converter program."
            return $false
        }

        # Ensure that file matches the hash
        if ((Get-FileHash -Path $fullFilePath -Algorithm MD5).Hash -ne $expectedFile.Hash) {
            Remove-Item -Path $fullFilePath -Force -Confirm:$false
            Write-Error "File '$($expectedFile.Name)' hash does not match the expected hash."
            return $false
        }
    }

    # Installation was successful
    Write-Information "Installed the XLS converter program."
    return $true
}