Reputation: 2842
I'm trying to create a function that will automatically install, update and import on demand a PowerShell module from repository PSGallery and this will be run on a build server so I have to avoid any kind of confirmation prompts. This function will be called quite often and will not try to install or update modules if the module is already loaded to the session (only the first time).
# Already imported? Let's not go further with updates and import again...
if (Get-Module -Name $moduleName)
{
Write-Host "'$moduleName' is already imported to the current session!"
return
}
If the module is not already installed, I will try to install it. The problem is when I get to the step that I have to install the NuGet PackageManagement provider (before importing PowerShellGet module needed to install modules from PSGallery). I do the following cmdlet:
Install-PackageProvider -Name "NuGet" -Confirm:$false -Verbose
But I get the following confirmation prompt:
I could solve this issue by using the -Force
parameter like that:
Install-PackageProvider -Name "NuGet" -Confirm:$false -Force -Verbose
But the problem I see with that solution (might not be a big deal) is that using -Force
will download and install again NuGet every time even if the installed version is up to date. Without the -Force
parameter, it will just skip it if the version is up to date and I would much rather that.
Is there a way to set the package source 'https://oneget.org/nuget-2.8.5.208.package.swidtag' as trusted so I don't get the confirmation prompt again without having to use -Force
parameter?
Upvotes: 3
Views: 3738
Reputation: 1
How about using the -ForceBoostrap parameter on Get-PackageProvider, e.g.
Get-PackageProvider -Name "nuget" -ForceBootstrap
Upvotes: 0
Reputation: 2342
Actually, if you run Get-PackageProvider -Name "NuGet" -Force
it will automatically download and install it if its not installed. If it is installed, it returns NuGet as an object. My PowerShell version is 5.1.14393.1480.
Original Response:
Maybe you could just check to see if its available and then run your command?
$NuGetProvider = Get-PackageProvider -Name "NuGet" -ErrorAction SilentlyContinue
if ( -not $NugetProvider )
{
Install-PackageProvider -Name "NuGet" -Confirm:$false -Force -Verbose
}
Upvotes: 1
Reputation: 444
Not sure if this will help, but here goes nothing:
Can you use the cmd version of try-catch using errorlevel?
Install-PackageProvider -Name "NuGet" -Confirm:$false -Verbose
if errorlevel 1 GOTO Forced
exit /b
:Forced
Install-PackageProvider -Name "NuGet" -Confirm:$false -Force -Verbose
Upvotes: 0