Inheriting Classes in PowerShell 5 (part 2)

by Mar 1, 2017

Here is another use case for the new class feature in PowerShell 5. In the previous example, we illustrated how you can derive new classes from System.Diagnostics.Process to get new and more powerful objects representing running processes.

Here’s a class that inherits from WebClient which is typically used to connect to websites. When you use the regular WebClient object, it refuses to connect to HTTPS sites with certificate errors. That’s a good thing, but sometimes you need to connect anyway.

#requires -Version 5

class MyWebClient : System.Net.WebClient
{
  MyWebClient() : base()
  {
    # with SSL certificate errors, connect anyway
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    $proxy = [System.Net.WebRequest]::GetSystemWebProxy()
    $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    $this.Proxy = $proxy
    $this.UseDefaultCredentials = $true
    $this.Proxy.Credentials = $this.Credentials
  }
}

$client = [mywebclient]::new()
$client.DownloadString('http://www.psconf.eu')

So this class “MyWebClient” inherits from WebClient() and changes the ServerCertificateValidationCallBack. It simply always returns $true, so all connections are successful, and the certificate check becomes irrelevant.

Twitter This Tip! ReTweet this Tip!