Invoke-UploadInstallerFiles.ps1

<#
.SYNOPSIS
    This script uploads the installer files to be converted.
 
.DESCRIPTION
    This script reads the package path, package version, package install arguments and
    package name and invokes upload MSIX REST APIs. The script will take a csv file as input.
 
.PARAMETER CsvPath
    Specifies a path to a location of the csv file.
 
.PARAMETER Env
    Optional parameter of the environment value.
 
.EXAMPLE
    C:\PS> .\Invoke-UploadInstallerFiles.ps1 -CsvPath "<%value%>"
 
.OUTPUTS
    The files gets uploaded from the provided location to the cloud.
 
#>


# Uncomment for debugging
# Set-PSDebug -Trace 2

# Create a temporary file which if needed will contain only the top 15 lines
$UploadCsvPath = "./uploadFilesTruncated.csv"

function Invoke-UploadInstallerFiles ()
{
    param(
        [Parameter(Mandatory, HelpMessage = "Please provide a valid path to the csv file")]
        [string]$CsvPath,

        [Parameter(HelpMessage = "Optional Parameter: Please provide the environment value.")]
        [string]$Env
    )

    Try
    {
        # Create the resource output file
        $CsvDirectoryPath = [System.IO.Path]::GetDirectoryName($CsvPath)
        $CsvName = [System.IO.Path]::GetFileNameWithoutExtension($CsvPath)
        $ResourceCsv = "${CsvName}_resource.csv"
        $ResourceCsvPath = Join-Path -Path $CsvDirectoryPath -ChildPath $ResourceCsv

        # Check for verbosity flags
        $Debug = $($PSCmdlet.MyInvocation.BoundParameters["Debug"] -eq $True)
        $Verbose = $($PSCmdlet.MyInvocation.BoundParameters["Verbose"] -eq $True)

        # 5 rows check
        if ($(Get-Content $CsvPath | Measure-Object â€“Line).Lines -gt 5)
        {
            Write-Warning "Data provided for more than 5 files, ignoring upload for 6th file and beyond."
            Get-ChildItem -Path $CsvPath -File | Where-Object { $_.Extension -match '\.(csv)'} | ForEach-Object { ($_ | Get-Content -TotalCount 6) | Set-Content -Path $UploadCsvPath }
            $CsvPath = $UploadCsvPath
        }

        # Authenticate user
        Authenticate($Env)
        $funcInvokeUploadInstaller = ${function:Invoke-UploadInstaller}.ToString()

        # Loading the file in memory
        $Files = Import-Csv -Path $CsvPath

        # Looping over the lines in CSV file
        $Files |
            ForEach-Object -ThrottleLimit 2 -Parallel {
                $PackagePath = $_.PackagePath
                $PackageVersion = $_.PackageVersion
                $PackageInstallArguments = $_.PackageInstallArguments
                $PublisherName = $_.PublisherName
                $PackageName = $_.PackageName
                $IsUnattendedInstallWithoutArgument = $_.IsUnattendedInstallWithoutArgument
         
                Write-Debug "PackagePath: $PackagePath ; PackageVersion: $PackageVersion ; PackageInstallArguments: $PackageInstallArguments ; PublisherName: $PublisherName ; PackageName: $PackageName ; IsUnattendedInstallWithoutArgument: $IsUnattendedInstallWithoutArgument" -Debug

                ${function:Invoke-UploadInstaller} = $using:funcInvokeUploadInstaller
                Invoke-UploadInstaller -PSScriptRoot $using:PSScriptRoot -PackagePath $PackagePath -PackageVersion $PackageVersion -PackageInstallArguments $PackageInstallArguments -Token $using:global:bearerToken -PublisherName $PublisherName -PackageName $PackageName -IsUnattendedInstallWithoutArgument $IsUnattendedInstallWithoutArgument -Env $using:Env -Debug:$Debug -Verbose:$Verbose

                [pscustomobject]@{ PackagePath = $PackagePath; PackageVersion = $PackageVersion; PackageInstallArguments = $PackageInstallArguments; PublisherName = $PublisherName; PackageName = $PackageName; IsUnattendedInstallWithoutArgument = $IsUnattendedInstallWithoutArgument; ConversionId =  $global:conversionId } | Export-Csv -Path $using:ResourceCsvPath -Append -NoTypeInformation
            }
        Write-Output "Completed. Check out the logs at" $("$env:temp\UploadFlowLogs")
    }
    Catch
    {
        Write-Output ($_)
        if (Test-Path $ResourceCsvPath)
        {
            Remove-Item -Path $ResourceCsvPath
        }
    }
    Finally
    {
        if (Test-Path $UploadCsvPath)
        {
            Remove-Item -Path $UploadCsvPath
        }
    }
}