Home > database >  Powershell - error CS0246: The type or namespace name 'ICertificatePolicy' could not be fo
Powershell - error CS0246: The type or namespace name 'ICertificatePolicy' could not be fo

Time:11-23

I am trying to reuse this powershell script which performs smoketest post deployment. The script works just fine when I run locally on the build server but when I try to run through my gitlab pipeline it fails with error: add-type @"(3,36): 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 {

Code that 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

$HTTP_Status_Timeout = 0
$HTTP_Request = [System.Net.WebRequest]::Create($url)

My understanding so far: Based on my research I tried to compare the Powershell version of my build server vs gitlab pipeline

Build server

Name                           Value
----                           -----
PSVersion                      5.1.14393.4583
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.4583
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3

gitlab pipeline

PSVersion                      7.2.4
PSEdition                      Core
GitCommitId                    7.2.4
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0.}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

As I understand the piece of code does not work in poweshell 7

What I have tried so far: I found out a similar issue here: PowerShell - Add-Type Issue trying to do REST call without self-signed cert issue

And I tried to add -SkipCertificateCheck but that did not work.
I also tried to force the powershell to use version 5.0 by adding #Requires -Version 5.0 to my pipeline code but that also dis not work

I also tried the code block but that results in a different error

$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,Tls13'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[ServerCertificateValidationCallback]::Ignore()
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

The above code is resulting in error: Exception setting "SecurityProtocol": "The requested security protocol is not supported." I tried to check the .Net version and the following versions are available

PSChildName : v2.0.50727
Version     : 2.0.50727.4927
PSChildName : v3.0
Version     : 3.0.30729.4926
PSChildName : Windows Communication Foundation
Version     : 3.0.4506.4926
PSChildName : Windows Presentation Foundation
Version     : 3.0.6920.4902
PSChildName : v3.5
Version     : 3.5.30729.4926
PSChildName : Client
Version     : 4.7.02053
PSChildName : Full
Version     : 4.7.02053
PSChildName : Client
Version     : 4.0.0.0

What I think could help:

Option1 - Making changes in the script which is compatible to run in Powershell 7.2
Option2 - Force my pipeline job to use powershell version 5

Although I think the 2 options can help resolving the problem but I am not sure if it is the right approach. Hence Request your guidance in finding the right solution/guidance in resolving this issue.

CodePudding user response:

Was able to resolve the issue and found 2 solutions to my problem

  1. Changed the gitlab-runner pointing from pwsh to powershell which will use the desktop/os version of powershell
  2. Curl can let you make a web-request and capture the return code. Here is the command curl $url | select-object statuscode

Hopefully this helps someone!

  • Related