@@ -11,19 +11,52 @@ $mob_url = "https://$vc_server/mob/?moid=VpxSettings&method=queryView"
11
11
$secpasswd = ConvertTo-SecureString $vc_password - AsPlainText - Force
12
12
$credential = New-Object System.Management.Automation.PSCredential($vc_username , $secpasswd )
13
13
14
- # Ingore SSL Warnings
15
- add-type - TypeDefinition @"
16
- using System.Net;
17
- using System.Security.Cryptography.X509Certificates;
18
- public class TrustAllCertsPolicy : ICertificatePolicy {
19
- public bool CheckValidationResult(
20
- ServicePoint srvPoint, X509Certificate certificate,
21
- WebRequest request, int certificateProblem) {
14
+ $Code = @'
15
+ using System;
16
+ using System.Collections.Generic;
17
+ using System.Net.Http;
18
+ using System.Net.Security;
19
+ using System.Security.Cryptography.X509Certificates;
20
+
21
+ namespace CertificateCapture
22
+ {
23
+ public class Utility
24
+ {
25
+ public static Func<HttpRequestMessage,X509Certificate2,X509Chain,SslPolicyErrors,Boolean> ValidationCallback =
26
+ (message, cert, chain, errors) => {
27
+ var newCert = new X509Certificate2(cert);
28
+ var newChain = new X509Chain();
29
+ newChain.Build(newCert);
30
+ CapturedCertificates.Add(new CapturedCertificate(){
31
+ Certificate = newCert,
32
+ CertificateChain = newChain,
33
+ PolicyErrors = errors,
34
+ URI = message.RequestUri
35
+ });
22
36
return true;
23
- }
24
- }
25
- "@
26
- [System.Net.ServicePointManager ]::CertificatePolicy = New-Object TrustAllCertsPolicy
37
+ };
38
+ public static List<CapturedCertificate> CapturedCertificates = new List<CapturedCertificate>();
39
+ }
40
+
41
+ public class CapturedCertificate
42
+ {
43
+ public X509Certificate2 Certificate { get; set; }
44
+ public X509Chain CertificateChain { get; set; }
45
+ public SslPolicyErrors PolicyErrors { get; set; }
46
+ public Uri URI { get; set; }
47
+ }
48
+ }
49
+ '@
50
+ if ($PSEdition -ne ' Core' ){
51
+ Add-Type - AssemblyName System.Net.Http
52
+ if (-not (" CertificateCapture" -as [type ])) {
53
+ Add-Type $Code - ReferencedAssemblies System.Net.Http
54
+ }
55
+ } else {
56
+ if (-not (" CertificateCapture" -as [type ])) {
57
+ Add-Type $Code
58
+ }
59
+ }
27
60
28
61
# Initial login to vSphere MOB using GET and store session using $vmware variable
29
62
$results = Invoke-WebRequest - Uri $mob_url - SessionVariable vmware - Credential $credential - Method GET
0 commit comments