Home > Software engineering >  creating a remote repo of powershellgallery.com - is this possible?
creating a remote repo of powershellgallery.com - is this possible?

Time:01-09

Has anyone tried to or know how to setup a remote or cached repo within Artifactory for the powershellgallery?

I am unable to find anything even remotely talking about this online as well.

PSGallery URI is: https://www.powershellgallery.com/api/v2

I am beginning to think it was intentionally made by MS to not allow situations like that (but am looking for confirmation if that's the case)

NOTE: the intent here is that we will then have an internally accessible remote repo that is a copy of the PSGallery. Which then allows us to make sure we are scanning the modules and abiding by compliance regulations.

when it's added to Artifactory as a remote repo, its successful, but is empty.

Then, we set it up on Windows machine with register-PSRepository -Name psgallery-remote -SourceLocation myartifactoryuri (successful)

But when trying to do a find-module -name InvokeBuild -repository psgallery-remote it fails with no results.

When doing find-module -name InvokeBuild -Repository PSGallery it's successful.

CodePudding user response:

You have to set up your repo and save PSGallery modules to it, then make it available to the local environment.

For a quick and dirty example to get stuff and use them:

New-Item -Path 'C:\' -Name 'LocalPSGallery' -ItemType Directory -Force -Verbose
Register-PSRepository -Name LocalPSGallery -SourceLocation 'C:\LocalPSGallery' -InstallationPolicy Trusted -Verbose
Get-PSRepository

Get-ChildItem -Path 'C:\LocalPSGallery'
# Results 
<#
Note it is empty because it should be.
The above commands do not create a clone, it just sets up you local repo pointer.
#>

# Select a set number of modules as a test or by named group, etc
Find-Module -Name '*' | 
Select-Object -First 9 |
Save-Module -Path 'C:\LocalPSGallery' -Force -ErrorAction SilentlyContinue 
Get-ChildItem -Path 'C:\LocalPSGallery' # requested modules are listed

# Note still no list, because no path to your repo
Find-Module -Name Carbon -Repository LocalPSGallery
# Results
<#
PackageManagement\Find-Package : No match was found for the specified search 
criteria and module name 'Carbon'. Try Get-PSRepository to see all available 
registered module repositories.
#>

# Add the path
$env:PSModulePath
$env:PSModulePath = "$env:PSModulePath;C:\LocalPSGallery"
$env:PSModulePath

# Note still no list, because you've not installed anything
Find-Module -Name Carbon -Repository LocalPSGallery
# Results
<#
PackageManagement\Find-Package : No match was found for the specified search 
criteria and module name 'Carbon'. Try Get-PSRepository to see all available 
registered module repositories.
#>

# Try and install your module first
Install-Module -Name Carbon -Repository 'LocalPSGallery' -Verbose
# Results
<#
VERBOSE: Repository details, Name = 'LocalPSGallery', Location = 'C:\LocalPSGallery'; IsTrusted = 'True'; IsRegistered = 'True'.
VERBOSE: Using the provider 'PowerShellGet' for searching packages.
VERBOSE: Using the specified source names : 'LocalPSGallery'.
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'.
VERBOSE: The specified Location is 'C:\LocalPSGallery' and PackageManagementProvider is 'NuGet'.
VERBOSE: Total package yield:'0' for the specified package 'Carbon'.

PackageManagement\Install-Package : No match was found for the specified search 
criteria and module name 'Carbon'. Try Get-PSRepository to see all 
available registered module repositories.
#>


# Direclty import via FQDN
Import-Module -FullyQualifiedName 'C:\LocalPSGallery\Carbon' -Force -Verbose
# Results
<#
VERBOSE: Loading module from path 'C:\LocalPSGallery\Carbon\2.12.0\Carbon.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\LocalPSGallery\Carbon\2.12.0\Carbon.format.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\LocalPSGallery\Carbon\2.12.0\Formats\Carbon.Security.HttpUrlAcl.format.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\LocalPSGallery\Carbon\2.12.0\Formats\Schedule.Service.RegisteredTask.format.ps1xml'.
VERBOSE: Populating RepositorySourceLocation property for module Carbon.
VERBOSE: Loading module from path 'C:\LocalPSGallery\Carbon\2.12.0\Carbon.psm1'.
VERBOSE: Importing function 'Add-CGroupMember'.
VERBOSE: Importing function 'Add-CTrustedHost'.
...
#>

Find-Module -Name Carbon -Repository LocalPSGallery
# Results
<#
PackageManagement\Find-Package : No match was found for the specified search criteria 
and module name 'Carbon'. Try Get-PSRepository to see all available 
registered module repositories.
#>

# The below will work as expected
Get-Module -Name Carbon
Get-Module -ListAvailable

Here is a blog on the use case as well.

MS does provide the full details offline PSGallery deployment, See: Working with Private PowerShellGet Repositories

Of course, downloading all modules is going to take a very long time, and again, has would need to be done regularly to stay current.

So, on a pristine system, and using the details from the MS doc, you'd end up with something link this:

# Create a new location for your repo
New-Item -Path 'C:\' -Name 'LocalPSGallery' -ItemType Directory -Force -Verbose

# Update your path for your repo
$env:PSModulePath
$env:PSModulePath = "$env:PSModulePath;C:\LocalPSGallery"
$env:PSModulePath

# While online, install required resources
'PackageManagement', 'OfflinePowerShellGetDeploy' | 
ForEach-Object {Install-Module -Name $PSitem -Force -Verbose}

Get-PSRepository

# Register a location for your repo
$CreateLocalPSGallery = @{
    Name                 = 'LocalPSGallery' 
    SourceLocation       = 'C:\LocalPSGallery' 
    ScriptSourceLocation = 'C:\LocalPSGallery' 
    InstallationPolicy   = 'Trusted'
}

Register-PSRepository @CreateLocalPSGallery -Verbose
Get-PSRepository

Get-ChildItem -Path 'C:\LocalPSGallery'

# Publish from the PSGallery to your local Repository
Find-Module -Name '*' | 
Select-Object -First 9 | 
ForEach-Object {
    $PublishFromLocalRepo = @{
        Name        = $PSItem.Name
        Provider    = 'NuGet' 
        Source      = 'https://www.powershellgallery.com/api/v2' 
        Path        = 'C:\LocalPSGallery'
        Force       = $True
        ErrorAction = 'SilentlyContinue'
    }

    Save-Package @PublishFromLocalRepo
}

# Validate resource download
Get-ChildItem -Path 'C:\LocalPSGallery'

# Validate all repo resources
Find-Module -Name '*' -Repository LocalPSGallery
Find-Module -Name '*json*' | 
Select-Object -First 3

# Make you repo the default - remote all other repos
Unregister-PSRepository -Name PSGallery -Verbose
Get-PSRepository

# Test your repo as the default
Find-Module -Name '*' -Repository LocalPSGallery
Find-Module -Name '*'

# After testing, reset the default
Register-PSRepository -Default -Verbose
Get-PSRepository

CodePudding user response:

You should configure Artifactory remote repository as the following :

Link for JFrog documentation on NuGet Remote Repositories : https://www.jfrog.com/confluence/display/JFROG/NuGet Repositories#NuGetRepositories-RemoteRepositories

  • Related