"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
        }
  4. In the same compose-init.ps1 file, find the following line:
    $SitecoreGalleryRepositoryLocation = "https://sitecore.myget.org/F/sc-powershell/api/v2"
  5. Replace it with the following line:
    $SitecoreGalleryRepositoryLocation = "https://nuget.sitecore.com/resources/v2"
  6. Save the changes, and the run the compose-init.ps1 script again.

    Note: The repository location must be updated because Sitecore public feeds have transitioned from MyGet to NuGet. For more information, refer to Transition of Sitecore public feeds from MyGet to NuGet after November 2023.

    Alternatively, download the Sitecore PowerShell Gallery from the Sitecore Installation Framework 2.4.1 download page.