Search code examples
powershellvisual-studio-codeinvoke-webrequest

Powershell ISE VSCODE Invoke-Webrequest difference


I am try to download the nuget.exe via Invoke-WebRequest inside a function.

If i use Windows Powershell ISE everything works fine.

$request = Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -UseBasicParsing -verbose
VERBOSE: GET https://dist.nuget.org/win-x86-commandline/latest/nuget.exe with 0-byte payload
VERBOSE: received 7088048-byte response of content type application/x-msdownload

If i use VSCODE Powershell extension i get.

$request = Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -UseBasicParsing -verbose
VERBOSE: GET https://dist.nuget.org/win-x86-commandline/latest/nuget.exe with 0-byte payload
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:12
+ $request = Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-com ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

I already tried

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 -bor [Net.SecurityProtocolType]::Ssl3

$PSVersionTable in ISE and VSCODE extension is identical

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1682
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Is there a way to analyze/debug the difference? There must be some kind of difference in this environments.

Other download variants besides Invoke-WebRequest won't work for me, because i need the $request answer to phrase the header in some variants


Solution

  • The error was a previous call to...

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 
    

    Setting the callback to $true won't work and screw things up.

    -SkipCertificateCheck is not availible in PSVersion 5.1.19041.1682

    Added some C# code to Set the ServerCertificateValidationCallback to true if needed.

    function Private-SslVerification {
        [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
        param()
        if (-not ([System.Management.Automation.PSTypeName]"SslVerification").Type)
        {
            Add-Type -TypeDefinition  @"
            using System.Net.Security;
            using System.Security.Cryptography.X509Certificates;
            public static class SslVerification
            {
                private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }
                public static void Disable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
                public static void Enable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
            }
    "@
        }
    }
    
    function Private-Disable-SslVerification
    {
        [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
        param()
        Private-SslVerification
        [SslVerification]::Disable()
    }
    function Private-Enable-SslVerification
    {
        [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
        param()
        Private-SslVerification
        [SslVerification]::Enable()
    }