Download Business Central Source Code Country Wise with PowerShell

 # Install BcContainerHelper (could be you need to run this as Administrator):

# Install-Module BcContainerHelper -force

# Configuration

$Countries = @('w1', 'in')  # List of countries

$Versions = @('26', '27')   # List of major versions (e.g., '24.0', '25.1', or just '25')

$Destination = 'C:\_Source\Microsoft\Artifacts'

$bcartifactsCacheFolder = 'c:\bcartifacts.cache\'

$openInCode = $false


foreach ($Country in $Countries) {

    foreach ($Version in $Versions) {

        Write-Host "--- Fetching: Country [$($Country.ToUpper())] | Version [$($Version)] ---" -ForegroundColor Cyan

        

        try {

            # 1. Get Artifact URL for specific version and country

            $ArtifactUrl = Get-BCArtifactUrl -version $Version -country $Country -select Latest

            

            # 2. Download Artifact

            $ArtifactPath = Download-Artifacts -artifactUrl $ArtifactUrl -includePlatform

            

            foreach ($path in $ArtifactPath) {

                Write-Host "Handling Path: $($path)" -ForegroundColor Green

                

                $AllSources = Get-ChildItem -Path $path -Recurse -Filter "*.source.zip"

                

                # Logic to maintain clean folder hierarchy: Destination\Country\Version

                $CountryFolder = join-path $Destination $Country

                $VersionSubFolder = join-path $CountryFolder $path.Substring($bcartifactsCacheFolder.Length)


                foreach ($source in $AllSources) {

                    $AppName = $source.Name.Replace('.Source.zip', '')

                    $DestinationFolder = join-path $VersionSubFolder $AppName


                    if (-not (Test-path $DestinationFolder)) {

                        Write-Host "Extracting $($source.name)..." -ForegroundColor Gray

                        Expand-Archive -Path $source.FullName -DestinationPath $DestinationFolder -Force

                    }

                }

                

                if ($openInCode) { code $VersionSubFolder }

            }

        }

        catch {

            Write-Warning "Could not find or download artifact for Country: $Country, Version: $Version"

        }

    }

}


Write-Host "All tasks completed!" -ForegroundColor Yellow

start $Destination

No comments:

Post a Comment