Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send

I was setting up a local on premises PSRepository server for collaboration purpose between my team also future planning is to limit users downloading modules from only from in-house PowerShell repositories. Local repository was setup but while publishing module on the repository I was getting below error.

Publish-Module -Path C:\On -Repository OnPremRepo -Force
Microsoft.PowerShell.Utility\Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7448 char:21
+ ...     $null = Microsoft.PowerShell.Utility\Invoke-WebRequest -Uri $scri ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Publish-Module : NuGet.exe is required to interact with NuGet-based repositories. Please ensure that NuGet.exe is available under one of the paths specified in PATH environment variable value.
At line:1 char:1
+ Publish-Module -Path C:\HW -Repository OnPremRepo -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Publish-Module], InvalidOperationException
    + FullyQualifiedErrorId : CouldNotInstallNuGetExe,Publish-Module

Related Articles
Get-PSRepository WARNING Unable to find module repositories
Creating an internal PowerShell module repository

After reading the complete error next statement caught into my eyes "Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send". This error statement is because of, by default PowerShell web request calls happens over TLS 1.0 version (TLS 1.0 is vulnerable and security best practice suggests it should be disabled), So you need to configure powershell to use TLS 1.2 version instead of the default TLS 1.0. 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Once I switched to TLS 1.2 version and tried publishing module and it was successful.

Publish-Module -Path C:\On -Repository OnPremRepo
NuGet.exe is Required to continue
PowerShellGet requires NuGet.exe to publish an item to the NuGet-based repositories. NuGet.exe must be available in 'C:\ProgramData\Microsoft\Windows\Powershell\PowerShellGet' or 'C:\Users\vcloud.VCLOUD-LAB\AppData\Local\Microsoft\Windows\PowerShell\PowerShellGet', or under one of the specified in PATH environment variable value. NuGet.exe can be downloaded from http://nuget.org/nuget.exe, do you want PowerShellGet to install NuGet.exe now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

Powershell Publish-module Net.ServicePointManager SecurityProtocol Net.SecurityProtocolType Tls12 Register-PSrepository remote psgallary local nuget module path environment powershellget to install nuget.exe PWSH.png

Useful Articles
GUI - SETUP AND CONFIGURE POWERSHELL WEB ACCESS SERVER (GATEWAY)
USE POWERSHELL ON MOBILE - SETUP AND CONFIGURE POWERSHELL WEB ACCESS (PSWA) 
Different ways to bypass Powershell execution policy :.ps1 cannot be loaded because running scripts is disabled
Powershell Trick : Execute or run any file as a script file
Set Powershell execution policy with Group Policy
Powershell execution policy setting is overridden by a policy defined at a more specific scope

Go Back



Comment

Blog Search

Page Views

4022028

Follow me on Blogarama