Check-PSGalleryApiAvailability fails when network is available #270
Description
Function that checks for PS Gallery API availability (link to dev branch here) is performing these three steps:
Test-Connection -ComputerName $microsoftDomain
Test-NetConnection -ComputerName $microsoftDomain
[System.Net.NetworkInformation.NetworkInterface]::GetIsNetworkAvailable()
Expected Behavior
If any of those commands returns $true
check should be successful.
Current Behavior
If some command throws an error, entire check will fail. Commands after failing one are not executed.
Possible Solution
We can catch exceptions thrown from these commands and proceed checking with next commands until we find connection or we do all three checks.
Something like:
if (command1available) {
try {$connected = Do-Check1}
catch {$connected = $false}
}
if ( -not $connected -and (command2available)) {
try {$connected = Do-Check2}
catch {$connected = $false}
}
if ( -not $connected -and (command3available)) {
try {$connected = Do-Check3}
catch {$connected = $false}
}
Or, another option would be to simply add -ErrorAction SilentlyContinue
to each check as $connected
is set to $false
in advance.
Steps to Reproduce (for bugs)
I encountered this issue while playing with Azure Functions.
Exact steps to identify this issue is to simply run these commands inside custom PowerShell Azure Function
Find-Module PowerShellGet
Install-Module PowerShellGet -Scope CurrentUser
First one will work fine, second one will fail on described error.
To further narrow down the error, you may run these commands:
[System.Net.NetworkInformation.NetworkInterface]::GetIsNetworkAvailable()
Test-NetConnection -ComputerName 'www.microsoft.com' -InformationLevel Quiet
First one will return $true
, while second one will throw an exception
[Error] Test-Connection : No such interface supported
Also, running Get-NetAdapter
command inside of the Azure Function will give you:
Get-NetAdapter : Cannot connect to CIM server. Access is denied
Your Environment
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.14393.2125
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.2125
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Get-Module -ListAvailable PowerShellGet,PackageManagement
Directory: D:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packag...
Script 1.0.0.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Directory: D:\Program Files (x86)\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packag...
Script 1.0.0.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Get-PackageProvider -ListAvailable
Name Version DynamicOptions
---- ------- --------------
msi 3.0.0.0 AdditionalArguments
msu 3.0.0.0
PowerShellGet 1.0.0.1 PackageManagementProvider, Type, Scope, AllowClobber, SkipPublisherCheck, I...
Programs 3.0.0.0 IncludeWindowsInstaller, IncludeSystemComponent
P.S.
I can do PR if needed, but I'd like to hear opinions first. Maybe this check is not needed at all?