Private/Receive-BusinessCentralDVD.ps1

# Will be called in VM
function Global:Receive-BusinessCentralDVD {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        Downloads the Business Central DVD and extracts it
    .DESCRIPTION
        ...
    #>

    param(
        [Parameter(Mandatory = $false, Position = 1)]
        [string]
        $Version,
        [Parameter(Mandatory = $false, Position = 2)]
        [string]
        $CumulativeUpdate,
        [Parameter(Mandatory = $false, Position = 3)]
        [string]
        $Language,
        [Parameter(Mandatory = $false, Position = 4)]
        [string]
        $DownloadDirectory = "C:\Install\"
    )
    process {
        $folderpath = $DownloadDirectory
        $filename = "BC DVD.zip"
        $dvdFilename = Join-Path $folderpath $filename

        $UrlToDownload = Get-BusinessCentralDownloadUrl -Version $Version -CumulativeUpdate $CumulativeUpdate -Language $Language
        
        if (!(Test-Path $folderpath)) {
            New-Item -ItemType Directory -Path  $folderpath | Out-Null
        }
        if (!(Test-Path $dvdFilename)) {
            Write-CustomHost -Message "Downloading NAV/BC DVD..."
            $clnt = New-Object System.Net.WebClient
            $clnt.DownloadFile($UrlToDownload, $dvdFilename)
            Write-CustomHost -Message "Download complete"
        }
        else { 
            Write-CustomHost -Message "File already exists."
        }

        # Unzip downloaded file
                
        $dvdPath = Join-Path $folderpath "DVD"
        if (-not (Test-Path (Join-Path $dvdPath 'setup.exe'))) {
            Write-CustomHost -Message "Unzipping $dvdFilename"
            Expand-Archive -Path $dvdFilename -DestinationPath $dvdPath
            Write-CustomHost -Message "Unzip complete"
            if (-not (Test-Path (Join-Path $dvdPath 'setup.exe'))) {
                # Get DVD-Zip from just extracted dir
                $dvdZipfile = Get-ChildItem -Path $dvdPath -Filter *.zip | Select-Object -First 1 | % { $_.FullName }

                # Move extracted ZIP to parent and delete remaining directory
                Write-CustomHost -Message "Cleaning up directory..."
                Move-Item $dvdZipfile $folderpath
                Remove-Item $dvdPath -Confirm:$false -Force -Recurse

                # Find moved file and Unzip it
                $dvdZipfile = Get-ChildItem -Path $folderpath -Filter *.zip | Where-Object { $_.Name -ne $filename } | Select-Object -First 1 | % { $_.FullName }

                Write-CustomHost -Message "Unzipping $dvdZipfile"
                Expand-Archive -Path $dvdZipfile -DestinationPath $dvdPath
                Write-CustomHost -Message "Unzip complete"

                # Remove previously extracted ZIP (from other ZIP)
                $dvdZipfile = Get-ChildItem -Path $folderpath -Filter *.DVD.zip | Where-Object { $_.Name -ne $filename } | Select-Object -First 1 | % { $_.FullName }
                Remove-Item $dvdZipfile -Confirm:$false -Force -Recurse
            }
        }
    }
}