This script will enable the PSGallery module repository to allow installation of other modules.
NuGet is required, due to this it will be installed first if required.
# Enable PSGallery repository for modules
#Requires -RunAsAdministrator
# Function to install/update NuGet
function Install-NuGet {
Write-Host -ForegroundColor Yellow 'Running install/upgrade command for NuGet...'
try {
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force -ErrorAction Stop
Write-Host -ForegroundColor Green "NuGet install/upgrade complete"
}
catch {
$message = $_.Exception.message
Write-Error 'Could not install NuGet. Try running this manually: Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force'
throw $message
}
}
# Function to install the PSGallery Repository
function Install-PSGallery {
Write-Host -ForegroundColor Yellow 'Running install of PSGallery repository...'
try {
Register-PSRepository -name 'PSGallery' -SourceLocation 'https://www.powershellgallery.com/api/v2' -PublishLocation 'https://www.powershellgallery.com/api/v2/package/' -ScriptSourceLocation 'https://www.powershellgallery.com/api/v2/items/psscript' -ScriptPublishLocation 'https://www.powershellgallery.com/api/v2/package/' -ErrorAction Stop
Write-Host -ForegroundColor Green 'PSGallery repository registered'
}
catch {
$message = $_.Exception.message
Write-Error 'Could not install PSGallery repository'
throw $message
}
}
# Check if NuGet is installed; install it if not
Write-Host -ForegroundColor Gray "Validating NuGet PackageProvider is available..."
try {
$version = (Get-PackageProvider -ListAvailable -Name NuGet -ErrorAction Stop).version
if ( $version -ge [Version]'2.8.5.201' ) {
Write-Host -ForegroundColor Green "NuGet PackageProvider available; version meets requirements"
} else {
Write-Host -ForegroundColor Yellow "NuGet version is outdated (version $version); upgrade will be attempted"
Install-NuGet
}
}
catch {
Write-Host -ForegroundColor Yellow 'NuGet does not appear to be installed. Installation will be attempted.'
Install-NuGet
}
# Get information about PSGallery
Write-Host -ForegroundColor Gray 'Checking PSGallery is installed...'
try {
$gallery = Get-PSRepository -Name PSGallery -ErrorAction Stop
Write-Host -ForegroundColor Gray 'PSGallery installed'
}
catch {
Write-Host -ForegroundColor Yellow 'PSGallery repository missing, attempting installation'
Install-PSGallery
$gallery = Get-PSRepository -Name PSGallery -ErrorAction Stop
}
# Check that it is registered
Write-Host -ForegroundColor Gray 'Checking PSGallery registration...'
if ( !$gallery.Registered ) {
Write-Host -ForegroundColor Yellow 'PSGallery repository not registered, attempting installation'
Install-PSGallery
} else {
Write-Host -ForegroundColor Gray 'PSGallery registered'
}
# Check that it is trusted
Write-Host -Verbose 'Checking PSGallery is trusted...'
if ( $gallery.Trusted ) {
Write-Host -ForegroundColor Gray 'PSGallery is trusted'
} else {
Write-Host -ForegroundColor Yellow 'Setting PSGallery to trusted'
try {
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop
Write-Host -ForegroundColor Green 'PSGallery is now trusted'
}
catch {
$message = $_.Exception.message
Write-Error 'Could not trust PSGallery:'
throw $message
}
}
# Done
Write-Host -ForegroundColor Green 'PSGallery setup is complete'