Solving SSL Connection Problems

by Jan 8, 2019

Sometimes when you try to access web services (or download internet content in general), PowerShell may bail out and complain about not being able to set up a protected SSL channel.

Let’s take a look at such an issue. Below is a code that is calling a web service connected to the German railway system. It is supposed to list the train stations in a given city:

$city = "Hannover"
$url = "https://rit.bahn.de/webservices/rvabuchung?WSDL"

$object = New-WebServiceProxy -Uri $url -Class db -Namespace test

$request = [test.SucheBahnhoefeAnfrage]::new() 
$request.bahnhofsname = $city
$response = $object.sucheBahnhoefe($request)
$response.bahnhoefe

Instead, it throws a number of exceptions related to SSL connection problems. Typically, these errors stem from two issues:

  • The SSL certificate used by the web site to establish the SSL connection is invalid, expired, or not trustworthy
  • The SSL connection requires a protocol currently not enabled

To solve problems associated with these issues, simply run these lines:

# ignore invalid SSL certificates
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

# allowing SSL protocols
$AllProtocols = [Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[Net.ServicePointManager]::SecurityProtocol = $AllProtocols

Once you do, for the current PowerShell session all is good. The above code now returns the train stations in the Hannover area, and if you dig deeper into the rich object model, you could even order your train tickets using PowerShell.

 
name                  nummer
----                  ------
Hannover Hbf         8000152
Hannover-Linden/Fi.  8002586
Hannover-Nordstadt   8002576
Hannover Bismarckstr 8002580
Hannover-Ledeburg    8002583
Hannover-Vinnhorst   8006089
Hannover-Leinhausen  8002585
Hannover Wiech-Allee 8002591
Hannover Ander.Misb. 8000578
Hannover Flughafen   8002589
Hannover-Kleefeld    8002584
Hannover-Bornum      8002581
Hann Münden          8006707
HannoverMesseLaatzen 8003487 
 

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!