Private/Get-Image.ps1

function Get-Image {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ImageUSBClass]$usbImage
    )
    $unzipSourcePath = Join-Path $usbImage.downloadPath -childpath  $usbImage.localPath
    $unzipDestPath = $unzipSourcePath -replace '.zip', ""
    Write-Host "Checking if file already downloaded." -ForegroundColor Cyan
    if (!(Test-Path "$usbImage.downloadPath\$usbImage.destFolder")) {
        New-Item -Path $usbImage.downloadPath -Name $usbImage.destFolder -ItemType Directory -Force | Out-Null
    }
    if (!(Test-Path -Path $unzipSourcePath)) {
        #Check space for unzip
        Test-DiskSpace -DriveLetter $unzipSourcePath.Split(":")[0]
        #Download Image.zip
        Write-Host "File not found, Downloading..." -ForegroundColor Cyan
        Write-Host "Downloading the Image $($usbImage.zipFile) in $unzipSourcePath"
        #Start-Process -filepath "$($usbImage.azcopyPath)\azcopy.exe" -ArgumentList "copy $($usbImage.url) $unzipSourcePath" -Wait
        #. $azCpPath copy $usbImage.url $unzipSourcePath
        Start-BitsTransfer -Source $usbImage.url -Destination $unzipSourcePath
    }
    else {
        Write-Host "File Downloaded" -ForegroundColor Green
        #Write-Host $unzipSourcePath
        $fso = New-Object -comobject Scripting.FileSystemObject
        $zipsize = ($fso.GetFile($unzipsourcepath)).size / 1GB
        Write-Host ".zip Size: $zipsize" -ForegroundColor Green
        if ($zipsize -le "0") {
            Remove-Item $unzipSourcePath
            Write-Host "File Error, Re-Downloading..."
            Write-Host "Downloading the Image $usbImage.zipFile in $unzipSourcePath" -ForegroundColor Cyan
            #Start-Process -FilePath "$($usbImage.azcopyPath)\azcopy.exe" -ArgumentList "copy $usbImage.url $unzipSourcePath"
            #. $azCpPath copy $usbImage.url $unzipSourcePath
            Start-BitsTransfer -Source $usbImage.url -Destination $unzipSourcePath
        }
        #Write-Host $zipsize
    }
    Write-Host "Checking if file already unzipped." -ForegroundColor Cyan
    if (!(Test-Path -Path $unzipDestPath)) {
        #Check space for unzip
        Test-DiskSpace -DriveLetter $unzipDestPath.Split(":")[0]
        try {
            #md $unzipDestPath
            Write-Host "Creating Image Directory" -ForegroundColor Cyan
            New-Item -Path $unzipDestPath -ItemType Directory | Out-Null
            Expand-Download $unzipSourcePath  $unzipDestPath
        }
        catch {
            Write-Warning $_.Exception
        }
    }
    else {
        Write-Host "File Unzipped" -ForegroundColor Green
        #Write-Host $unzipSourcePath
        $fso = New-Object -comobject Scripting.FileSystemObject
        $size = ($fso.GetFolder($unzipDestPath)).size / 1GB
        Write-Host "Folder Size: $size" -ForegroundColor Green
        if ($size -le ($fso.GetFile($unzipSourcePath)).size / 1GB) {
            Write-Host "File Error, Re-Unzipping..." -ForegroundColor Red
            Write-Host "Deleting $unzipDestPath" -ForegroundColor Cyan
            Remove-Item $unzipDestPath -Recurse -Force
            try {
                #md $unzipDestPath
                Write-Host "Creating Image Directory" -ForegroundColor Cyan
                New-Item -Path $unzipDestPath -ItemType Directory -Force | Out-Null
                Write-Host "Unziping Image" -ForegroundColor Cyan
                Expand-Download $unzipSourcePath  $unzipDestPath
            }
            catch {
                Write-Warning $_.Exception.Message
            }
        }
    }
    $usbImage.DirCount = ( Get-ChildItem $unzipDestPath | Measure-Object ).Count
    if ($usbImage.DirCount -eq 1) {
        $usbImage.dirName = "BOOTME"
    }
    else {
        $usbImage.dirName = 'WinPE'
        $usbImage.dirName2 = 'Images'
    }
    return $usbImage
}