Skip to content

Commit 0a17c35

Browse files
author
William Lam
committed
Updated script to support latest PowerShell Core
1 parent 6af8cea commit 0a17c35

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

powershell/automate-vsphere-mob.ps1

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,52 @@ $mob_url = "https://$vc_server/mob/?moid=VpxSettings&method=queryView"
1111
$secpasswd = ConvertTo-SecureString $vc_password -AsPlainText -Force
1212
$credential = New-Object System.Management.Automation.PSCredential($vc_username, $secpasswd)
1313

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+
});
2236
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+
}
2760

2861
# Initial login to vSphere MOB using GET and store session using $vmware variable
2962
$results = Invoke-WebRequest -Uri $mob_url -SessionVariable vmware -Credential $credential -Method GET

0 commit comments

Comments
 (0)