Find-Module fails when invoked within scriptblock, when PSGallery inaccessible #249
Description
This one is a bit difficult to reproduce, that's why it took me a while to hunt down...
Expected Behavior
This should 'just work':
{Find-Module -Name PSReadline}.Invoke()
Current Behavior
In short, in some cases I'll detail below, running a {Find-Module -Name PSReadline}.Invoke()
fails when ran behind a proxy blocking access to the PowerShellGallery, despite having 2 PS Repository set for that account, and the module PSReadline available (on 1 of the repository).
Now, running Find-Module -Name PSReadline
works fine, so does {Find-Module -Name PSReadline -Repository MyRepositoryName}.invoke()
.
This seems to happens when:
- PowerShellGet is not installed for all Users (I use Save-Module in separate process)
- PSRepository PSGallery is unregistered
Possible Solution
The reason seems to be that Find-Module is calling Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue
, even when that Gallery is unregistered, and the -EA SilentlyContinue
seems to be ignored.
Changing the lines to the following fixes it for me (forcing terminating error and handling exception):
try {
$psgalleryRepo = Get-PSRepository -Name $Script:PSGalleryModuleSource `
-ErrorAction Stop `
-WarningAction SilentlyContinue
}
catch {
$psgalleryRepo = $false
}
Steps to Reproduce
On a Windows 7 / Windows 2012R2 with WMF 5.1 installed, with default PowerShellget 1.0.0.1, behind proxy:
On a separate Process:
C:\> Save-Module PackageManagement,PowerShellGet -Path C:\tmp\
On the testing process
$Env:PSmodulePath = 'C:\tmp;' + $Env:PSModulePath
C:\> Import-Module PowerShellGet -Force
C:\> Import-PackageProvider PowerShellGet -force -requiredVersion 1.6.0
C:\> Find-Module PSReadline # works
C:\> {Find-Module PSReadline}.Invoke() #fails
C:\> Get-PSRepository PSGallery -ErrorAction SilentlyContinue # works
C:\> {Get-PSRepository PSGallery -ErrorAction SilentlyContinue}.Invoke() #fails
If you have PowerShellGet 1.6.0 installed in 'C:\Program Files\WindowsPowerShell\Modules' , and is imported from there, it won't error (I have no idea why...).
I could not reproduce this on my win 10 laptop by unplugging the network (It's not behind a proxy, so not ideal test case anyway).
Context
This is used when fetching some Configuration Data (latest module Version available on internal feed) during DSC MOF compilation.
Your Environment
PSVersion 5.1.14409.1012
Module PackageManagement 1.1.7.0
Module PowerShellGet 1.6.0