Share via


I have a script works on a Windows 10 Pro Computer but not a Windows Server 2012 R12 machine

Question

Monday, July 8, 2019 8:46 PM

First I do have powershell 5.1 installed on both.
Second I do have .Net Framework 4.7 on both.
Script works without error in windows 10
Error on Server 2012 R2 VM:
Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occurred on a receive."

Any help is appriciated

#Download Files
$username = "Username"
$password = "Password"

$url = "https://mywebsite-support.com/Usr/Main/3rdParty/Myzipfile.zip"
$path = "E:\Install\3rdParty\Myzipfile.zip"

$web = New-Object System.Net.WebClient
$web.Credentials = new-object System.Net.NetworkCredential($username, $password)

Try
{
$web.DownloadFile($url, $path)
}
Catch
{
$ErrorMessage = $_.Exception.Message
$ErrorMessage
}

All replies (15)

Monday, July 8, 2019 10:19 PM ✅Answered

Final working code:

#Download Files
$username = "BBobby"
$password = "Password123"

$url = "https://mywebite.com/Usr/BBobby/3rdParty/MyZipFile.zip"
$path = "E:\Install\3rdParty\MyZipFile"

#[Net.ServicePointManager]::SecurityProtocol += 'Tls12'
$web = New-Object System.Net.WebClient
$web.Credentials = new-object System.Net.NetworkCredential($username, $password)
$web.DownloadFile($url, $path)

Monday, July 8, 2019 8:47 PM

Will add I can open up web browser on both and go to website and view all files


Monday, July 8, 2019 9:17 PM

Web site requires TLS 1.2.  TLS 1.2 is not enabled on WS2012r2 and earlier systems.

[Net.ServicePointManager]::SecurityProtocol

\(ツ)_/


Monday, July 8, 2019 9:23 PM

Web site requires TLS 1.2.  TLS 1.2 is not enabled on WS2012r2 and earlier systems.

[Net.ServicePointManager]::SecurityProtocol

\(ツ)_/

So no way to fix this?


Monday, July 8, 2019 9:27 PM | 1 vote

Hi,

You can enable TLS 1.2 if you have it disabled:
/en-us/windows-server/security/tls/tls-registry-settings#tls-12

An easy way to do so, is with the IIS Crypto free tool.

Here's also the supported protocols in TLS/SSL for various of Windows versions:
Protocols in TLS/SSL (Schannel SSP)

Best regards,
Leon

Blog: https://thesystemcenterblog.com LinkedIn:


Monday, July 8, 2019 9:31 PM | 1 vote

No .  Just set the protocol.

[Net.ServicePointManager]::SecurityProtocol += 'Tls12'

No need for any fancy tools.

SSL - all versions are no longer supported for any version of Windows.  They may exist but disabling most is recommended or required for security reasons.   They should/must be disabled in IE.

Almost all web services and sites are now only supporting Tls 1.2.

\(ツ)_/


Monday, July 8, 2019 9:37 PM | 1 vote

Also note that some sites may still require SSL 3.  W10Pro disables that by default but it can be added in the same way.

\(ツ)_/


Monday, July 8, 2019 9:39 PM

No .  Just set the protocol.

[Net.ServicePointManager]::SecurityProtocol += 'Tls12'

No need for any fancy tools.

SSL - all versions are no longer supported for any version of Windows.  They may exist but disabling most is recommended or required for security reasons.   They should/must be disabled in IE.

Almost all web services and sites are now only supporting Tls 1.2.

\(ツ)_/

Not sure where to add the [Net.servicePointManager]...etc] but I get error: The remote server returned an error: (401) Unauthorized.

#Download Files
$username = "xxxx"
$username = "xxxx"

$url = "https://transact-xxxx.com/Usr/gbowlsby/3rdParty/Additional_Support_files.zip"
$path = "E:\Install\3rdParty\Additional_Support_files.zip"

[Net.ServicePointManager]::SecurityProtocol += 'Tls12'
$web = New-Object System.Net.WebClient
$web.Credentials = new-object System.Net.NetworkCredential($username, $password)
$web.DownloadFile($url, $path)

Monday, July 8, 2019 9:45 PM | 1 vote

You cannot use Windows credentials to non-Windows services.  You must contact the site and find out how to authenticate if it is allowed.

The TLS obviously worked.

\(ツ)_/


Monday, July 8, 2019 9:47 PM

You cannot use Windows credentials to non-Windows services.  You must contact the site and find out how to authenticate if it is allowed.

The TLS obviously worked.

\(ツ)_/

It uses the SFTP - SSH File Transfer Protocol. 

I can log in through Filezilla. Just wanted to automate some processes no port, logon type normal


Monday, July 8, 2019 9:49 PM

DownloadFile does not support SFTP.  You will need to use a third party solution.

\(ツ)_/


Monday, July 8, 2019 9:52 PM

DownloadFile does not support SFTP.  You will need to use a third party solution.

\(ツ)_/

Actually I got it, edit the regisitry to disabled the tls and removed the[Net.ServicePointManager]::SecurityProtocol += 'Tls12'.

Then it worked


Monday, July 8, 2019 9:55 PM | 1 vote

DownloadFile does not support SFTP.  You will need to use a third party solution.

\(ツ)_/

Actually I got it, edit the regisitry to disabled the tls and removed the[Net.ServicePointManager]::SecurityProtocol += 'Tls12'.

Then it worked

You didn't need to do that and SFTP is not the method required.  Do NOT disable TLS in the registry.  That was no the issue.  Somewhere you gave us bad information.  Now you will always fail on all sites that require TLS.

Without accurate technical information there is no way to analyze your issue. 

\(ツ)_/


Monday, July 8, 2019 10:12 PM

DownloadFile does not support SFTP.  You will need to use a third party solution.

\(ツ)_/

Actually I got it, edit the regisitry to disabled the tls and removed the[Net.ServicePointManager]::SecurityProtocol += 'Tls12'.

Then it worked

You didn't need to do that and SFTP is not the method required.  Do NOT disable TLS in the registry.  That was no the issue.  Somewhere you gave us bad information.  Now you will always fail on all sites that require TLS.

Without accurate technical information there is no way to analyze your issue. 

\(ツ)_/

Right now I am only messing around in a Test VM. We have scripts that disable the TLS in the registery for what we do. 


Monday, July 8, 2019 10:17 PM

DownloadFile does not support SFTP.  You will need to use a third party solution.

\(ツ)_/

Actually I got it, edit the regisitry to disabled the tls and removed the[Net.ServicePointManager]::SecurityProtocol += 'Tls12'.

Then it worked

You didn't need to do that and SFTP is not the method required.  Do NOT disable TLS in the registry.  That was no the issue.  Somewhere you gave us bad information.  Now you will always fail on all sites that require TLS.

Without accurate technical information there is no way to analyze your issue. 

\(ツ)_/

Re-enabled tls and confirmed still working. Thank you for your help