"Module Repository 'SitecoreGallery' exists." error during the "compose-init.ps1" script execution


Description

Running the compose-init.ps1 script might result in the following error in the log records: 

PackageManagement\Register-PackageSource : Module Repository 'SitecoreGallery' exists.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.1.4\PSModule.psm1:11325 char:17
+ ...     $null = PackageManagement\Register-PackageSource @PSBoundParamete ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (Microsoft.Power...erPackageSource:RegisterPackageSource) [Register-PackageSource], Exception
  + FullyQualifiedErrorId : PackageSourceExists,Microsoft.PowerShell.PackageManagement.Cmdlets.RegisterPackageSource

The error occurs if the script is run more than one time on the same machine because the SitecoreGallery PSGallery has been registered before.

Apart from displaying the error, the bug should not cause any other issues while running the script. The .env values and certificates are initialized properly. Therefore, it is safe to ignore the error produced by the script. If you want to get rid of the error consider applying the following solution.

Solution

As a solution, consider the following steps:

  1. Open the compose-init.ps1 file.
  2. Find the following code:
    # Check for Sitecore Gallery
        Import-Module PowerShellGet
        $SitecoreGallery = Get-PSRepository | Where-Object { $_.SourceLocation -match 'Sitecore_Gallery' }
        if (-not $SitecoreGallery) {
            Write-Host "Adding Sitecore PowerShell Gallery..." -ForegroundColor Green
            Register-PSRepository -Name SitecoreGallery -SourceLocation $SitecoreGalleryRepositoryLocation -InstallationPolicy Trusted
            $SitecoreGallery = Get-PSRepository -Name SitecoreGallery
        }
  3. Replace it with the proposed solution:
     # Check for Sitecore Gallery
        Import-Module PowerShellGet        
        $SitecoreGalleryName = 'SitecoreGallery'
        $SitecoreGallery = Get-PSRepository | Where-Object { $_.Name -eq $SitecoreGalleryName }
        if (-not $SitecoreGallery) {
            Write-Host "Adding Sitecore PowerShell Gallery..." -ForegroundColor Green
            Register-PSRepository -Name $SitecoreGalleryName -SourceLocation $SitecoreGalleryRepositoryLocation -InstallationPolicy Trusted
            $SitecoreGallery = Get-PSRepository -Name $SitecoreGalleryName
        }