Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerShell 7 (x64) error #59

Closed
egilder opened this issue Jan 26, 2021 · 2 comments
Closed

PowerShell 7 (x64) error #59

egilder opened this issue Jan 26, 2021 · 2 comments

Comments

@egilder
Copy link

egilder commented Jan 26, 2021

If ($Null -eq ([System.Management.Automation.PSTypeName]'TrustAllCertsPolicy').Type) {
Add-Type -Debug:$False @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
}

It appears that PSVersion 7.1.1 isn't able to load or find the type/namespace for ICertificatePolicy. PSVersion 5.1.1 operates as it's been just fine. The error outputs are as follows.

PS C:\Users\(new-object Net.WebClient).DownloadString('https://bit.ly/LTPoSh') | iex;
Add-Type:
Line |
  91 |      Add-Type -Debug:$False @"
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~
     | (3,44): error CS0246: The type or namespace name 'ICertificatePolicy' could not be found (are you missing a using directive or an assembly reference?)
        public class TrustAllCertsPolicy : ICertificatePolicy {
                                           ^

Add-Type:
Line |
  91 |      Add-Type -Debug:$False @"
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot add type. Compilation errors occurred.
New-Object:
Line |
 103 |  … vicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
     |                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find type [TrustAllCertsPolicy]: verify that the assembly containing this type is loaded.
@egilder
Copy link
Author

egilder commented Jan 26, 2021

I was able to get it to work with modifying lines 90-103 and replacing them with the following code.

    $certCallback = @"
    using System;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    public class ServerCertificateValidationCallback
    {
        public static void Ignore()
        {
            if(ServicePointManager.ServerCertificateValidationCallback ==null)
            {
                ServicePointManager.ServerCertificateValidationCallback += 
                    delegate
                    (
                        Object obj, 
                        X509Certificate certificate, 
                        X509Chain chain, 
                        SslPolicyErrors errors
                    )
                    {
                        return true;
                    };
            }
        }
    }
"@
    Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()

ben-graemouse added a commit to ben-graemouse/LabTech-Powershell-Module that referenced this issue Aug 26, 2022
@ssamantasinghar
Copy link

I have a similar issue and here is the peice of code which is erroring out

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Based on your suggestion above I replaced my code with your code

$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
	public static void Ignore()
	{
		if(ServicePointManager.ServerCertificateValidationCallback ==null)
		{
			ServicePointManager.ServerCertificateValidationCallback += 
				delegate
				(
					Object obj, 
					X509Certificate certificate, 
					X509Chain chain, 
					SslPolicyErrors errors
				)
				{
					return true;
				};
		}
	}
}
"@
Add-Type $certCallback


$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[ServerCertificateValidationCallback]::Ignore()

It is still erroring out although with a different error :
Exception setting "SecurityProtocol": "The requested security protocol is not supported."

Any suggestion on how this issue can be resolved?

@egilder egilder closed this as completed Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants