PowerShell TIP – Exception calling “DownloadString” with “1” argument(s)

Sharing the recent issues that we have faced with some of our older scripts that reads/download XML using System.Net.WebClient and report on system health status.(Ironport)

All the scripts suddenly started throwing below error:

Exception calling “DownloadString” with “1” argument(s): “The underlying connection was closed: An unexpected error occurred on a send.”

On researching on internet , We have found its because our scripts are not supporting TLS 1.2 but good news is, by just adding one liner to your script will resolve this issue.

Here is the line in bold that we have added before calling webclient for the resolution.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Script Sample code.

############Call Web clientt############

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$WebClient = new-object System.Net.WebClient
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$WebClient.Credentials = new-object System.Net.NetworkCredential($Userid, $password)
[xml]$XMLIronport1 = $webclient.DownloadString($IronPort1)

######################################

Here is the old script that we were using in our environment for Ironport Health Check that can be fixed by using this technique.

https://gallery.technet.microsoft.com/scriptcenter/Ironport-Health-Check-22daa2db

If you are in the Same situation than this short post will assist you resolving this error.

 

Thanks for reading

Sukhija Vikas

http://SysCloudPro.com

 

 

2 thoughts on “PowerShell TIP – Exception calling “DownloadString” with “1” argument(s)

Leave a comment