Trying to register a psrepository behind a corporate proxy fails on Powershell Core.
1 2 3 4 5 6 7 8 9 10 11 |
$credential = Get-Credential $uri = 'https://localhost:8080/artifactory/api/nuget/custom_repository' $repoParams = @{ Name = 'custom_repository' SourceLocation = $uri PublishLocation = $uri InstallationPolicy = 'Trusted' PackageManagementProvider = 'NuGet' Verbose = $true } Register-PSRepository @repoParams -Credential $credential |
The error you receive is:
1 |
Register-PSRepository: The specified Uri 'https://localhost:8080/artifactory/api/nuget/custom_repository' for parameter 'SourceLocation' is an invalid Web Uri. Please ensure that it meets the Web Uri requirements. |
Running the same on Windows Powershell 5.1 works.
Now, let see why it’s not working on Powershell Core.
Trying to find more details about the error message:
1 |
$Error[0] | Select-Object * |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
PSMessageDetails : Exception : System.ArgumentException: The specified Uri 'https://localhost:8080/artifactory/api/nuget/custom_repository' for parameter 'SourceLocation' is an invalid Web Uri. Please ensure that it meets the Web Uri requirements. at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord) TargetObject : https://localhost:8080/artifactory/api/nuget/custom_repository CategoryInfo : InvalidArgument: (https://localhost…l-custom_repository:String) [Register-PSRepository], ArgumentException FullyQualifiedErrorId : InvalidWebUri,Register-PSRepository ErrorDetails : InvocationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at ThrowError, C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1: line 7113 at Resolve-Location, C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1: line 6121 at Register-PSRepository, C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1: line 11423 at , : line 1 PipelineIterationInfo : {} |
Let’s look at the function inside the module where the error is triggered (bonus tip on how to jump directly to the line number)
1 |
code -g 'C:\program files\powershell\7\Modules\PowerShellGet\PSModule.psm1:6121' |
The error message is misleading, as it turns out the part that is failing is this one:
1 |
$pingResult = Ping-Endpoint -Endpoint $Location -Credential $Credential -Proxy $Proxy -ProxyCredential $ProxyCredential |
We know the endpoint location is valid, so that means we cannot pass through the proxy.
Digging around the internet led me to this GitHub issue.
Apparently, .NET Core is not respecting proxy in some cases.
The recommended workaround from Steve Lee is to set
1 |
[System.AppContext]::SetSwitch("System.Net.Http.UseSocketsHttpHandler", $false) |
or to create
1 |
$env:DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER = 0 |
Turns out our little problem is now fixed and the Register-PSRepository
command runs successfully.
I hope this post will save you some time in case you run into the same issue.
Voilà!