Private/Helper/Test-IntegrisFileDependency.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Test-IntegrisFileDependency {
    <#
    .SYNOPSIS
    Tests and ensures the specified file is present, downloading it if necessary.
 
    .DESCRIPTION
    This function checks if a specified file is present at a given path. If the file is not found, it prompts the user to download it from a specified URL, with options to unzip the file and force the download.
 
    .PARAMETER RootPath
    Specifies the root path where the file should be located.
 
    .PARAMETER ChildPath
    Specifies the child path relative to the root path where the file should be located.
 
    .PARAMETER DownloadURL
    Specifies the URL from which to download the file if it is not present.
 
    .PARAMETER Unzip
    Indicates whether the downloaded file should be unzipped.
 
    .PARAMETER Confirm
    Prompts the user for confirmation before downloading the file.
 
    .PARAMETER Force
    Forces the download of the file even if it is already present.
 
    .EXAMPLE
    Test-IntegrisFileDependency -RootPath "C:\Files" -ChildPath "example.zip" -DownloadURL "http://example.com/file.zip" -Unzip -Confirm -Force
    Tests and ensures the specified file is present, downloading and unzipping it if necessary, with user confirmation and force options.
 
    .NOTES
    The function sets the TLS protocol, checks for the file, and handles download and extraction as needed.
    #>


    PARAM (
        [Parameter(Mandatory)]
        [String]$RootPath,
        [Parameter(Mandatory)]
        [String]$ChildPath,
        [Parameter(Mandatory)]
        [String]$DownloadURL,
        [Switch]$Unzip,
        [Switch]$Confirm,
        [Switch]$Force
    )

    # Set TLS Protocol for PowerShell Session
    TRY { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } CATCH {}

    # Setup FilePath variable
    $FilePath = Join-Path -Path $RootPath -ChildPath $ChildPath

    # Check if File is present
    $FilePresent = Test-Path -Path $FilePath

    IF ($FilePresent) {
        RETURN $True
    }
    ELSEIF (!($FilePresent) -or $Force) {
        # Check if user consent is required
        IF ($Confirm -and !($Force)) {
            Write-Host "$($FilePath) is requied for this Function, do you want to download it?" -ForegroundColor Yellow
            $UserInputInstall = Read-Host "Do you want to download it? [Y] Yes [N] No "
            IF ($UserInputInstall -match "[yY]") {
                $Download = $True
            }
            ELSE {
                $Download = $False
            }
        }

        # If user consent is granted, proceed to download
        IF ($Download -or !($Confirm) -or $Force) {
            # Create parent folders, if they need to be created
            IF (!(Test-Path -Path $RootPath -PathType Container)) {
                New-Item -ItemType Directory -Force -Path $RootPath -Force
            }

            # Set Download Path
            IF ($Unzip) { $DownloadPath = Join-Path -Path $RootPath -ChildPath "temp.zip" }
            ELSE { $DownloadPath = $FilePath }

            # Download file
            Write-Host "Downloading File ""$($DownloadPath)"" from URL: $($DownloadURL)" -ForegroundColor Cyan
            Invoke-WebRequest $DownloadURL -OutFile $DownloadPath

            # Unzip archive, if necessary
            IF ($Unzip) {
                Expand-Archive -Path $DownloadPath -DestinationPath $RootPath -Force
                Remove-Item -Path $DownloadPath | Out-Null
            }

            # Test if the download was successful
            $FilePresent = Test-Path -Path $FilePath
            IF ($FilePresent) {
                RETURN $True
            }
            ELSE {
                Write-Error "Error downloading $($FilePath). Please install manually."
                RETURN $False
            }
        }
        ELSE {
            Write-Error "Please install $($FilePath) manually."
            RETURN $False
        }
    }
}