how to fix DownloadFile error in powershell in windows10 [closed] - powershell

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I ran cmd as administrator and input:
start powershell
when open the powershell, I input:
$client = new-object System.Net.WebClient
$client.DownloadFile("https://www.cse.ust.hk/msbd5003/data/fruits.txt","D:\IT\fruits.txt")
here is the error information:
Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
At line:1 char:1
+ $client.DownloadFile("https://www.cse.ust.hk/msbd5003/data/fruits.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I tried adding $ client.Credentials = Get-Credential while it did not work.

My guess would be that since you're connecting through https you may need to set a more secure schannel protocol to communicate with the site you're trying to download from.
add this line indicating security protocol before your webclient request
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$client = new-object System.Net.WebClient
$client.DownloadFile("https://www.cse.ust.hk/msbd5003/data/fruits.txt","D:\IT\fruits.txt")
Alternately, if you are only running this script from one machine, you can set .NET to use secure protocols by default.
You can read more about it at https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls
Key - HKEY_LOCAL_MACHINE\SOFTWARE\[Wow6432Node\]Microsoft\.NETFramework\<VERSION>
Name - SchUseStrongCrypto
Value - 1
Also, the native PowerShell cmdlet invoke-webrequest may simplify your request.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri "https://www.cse.ust.hk/msbd5003/data/fruits.txt" -OutFile "D:\IT\fruits.txt"

Related

kubernetes - "kubectl" installation fails on Windows from PowerShell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
Installation on windows should be very simple from the docs https://kubernetes.io/docs/tasks/tools/install-kubectl but mine fails at second step
Install-Script -Name 'install-kubectl' -Scope CurrentUser -Force
install-kubectl.ps1 [-DownloadLocation <path>]
install-kubectl.ps1 c:\kubectl
==>Getting download link from https://kubernetes.io/docs/tasks/tools/install-kubectl/
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Program Files\PowerShell\Scripts\install-kubectl.ps1:31 char:8
+ $req = Invoke-WebRequest -UseBasicParsing -Uri $uri
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
It seems like it is trying to download something from https://kubernetes.io/docs/tasks/tools/install-kubectl/ but it can not.
I succeeded to check that connection, I do have access to internet from that Windows Server:
PS C:\Users\e561> Test-NetConnection -ComputerName "kubernetes.io" -Port 443
ComputerName : kubernetes.io
RemoteAddress : 147.75.40.148
RemotePort : 443
InterfaceAlias : Ethernet 2
SourceAddress : 10.7.147.211
TcpTestSucceeded : True
As mentioned in the comments :
You can download kubectl.exe from here - and later add the path kubectl.exe located to environment variables.

Connect-MsolService : An error occurred while making the HTTP request to https://provisioningapi.microsoftonline.com/provisioningwebservice.svc

Why I'm getting this error randomly .,Powershell script sometimes working properly sometime not working throwing below error.
Connect-MsolService : An error occurred while making the HTTP request to
https://provisioningapi.microsoftonline.com/provisioningwebservice.svc. This could be due to the fact that the server
certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the
security binding between the client and the server.
At D:\O365License\Licensing.ps1:28 char:1
+ Connect-MsolService -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Connect-MsolService], CommunicationException
+ FullyQualifiedErrorId :
System.ServiceModel.CommunicationException,Microsoft.Online.Administration.Automation.ConnectMsolService
You might be suffering from a .NET related issue with TLS 1.2. Try running this before you run the Connect-MsolService command.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

powershell Accepting self-signed certificates using ServerCertificateValidationCallback

I cannot seem to get this to work using Powershell 5.1. The device is a Cisco MX800 CE9.3.
$url = "https://10.1.135.20/getxml?location=/Status"
[Net.ServicePointManager]::ServerCertificateValidationCallback={$true}
$webclient = New-Object System.Net.Webclient
$credCache = New-Object System.Net.CredentialCache
$creds = New-Object System.Net.NetworkCredential($user,$pwd)
$credCache.Add($url, "Basic", $creds)
$webclient.Credentials = $credCache
$webpage = $webclient.DownloadString($url)
Running this script using http returns XML as expected, but using https returns the error below
Exception calling "DownloadString" with "1" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
In the case above, after searching for answers, I dug in and did packet captures.
One packet capture with powershell talking to the server and one packet capture with a web browser talking to the server.
The PS Client Hello was using TLS1.0
The Web browsers Client Hello was using TLS1.2
So, in PS I added this to the code and I was able to use https against the server.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Upload files using FTP in PowerShell Scripts Fail [duplicate]

This question already has answers here:
"Requested URI is invalid" during upload with FtpWebRequest
(2 answers)
Closed 4 years ago.
I am using PowerShell Scripts to upload the files by FTP. The scripts I use is following:
$File = "C:\test\test01.txt"
$ftp = "ftp://MyUsername:MyPassword#MyHostName/"
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Uploading $File..."
$webclient.UploadFile($uri,$File)
But When I run the scripts, I get the following errors:
Exception calling "UploadFile" with "2" argument(s): "The requested URI is invalid for this FTP command."
At C:\FTP.ps1:11 char:1
+ $webclient.UploadFile($uri,$File)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
The Powershell Version I use is following:
Major Minor Build Revision
----- ----- ----- --------
3 0 -1 -1
Anyone knows how to solve this problem?
Thanks
A possible explenation could be that your username/password contains special characters that are not allowed in a valid URI (such as # or :).
Instead you should set your credentials using System.Net.NetworkCredential
See this answer for reference

Powershell will not allow ServerCertificateValidationCallback change

I'm try to make a call with Invoke-RestMethod but the server I'm connecting to has a self-signed certificate.
After some searching I found that I need to run the following to make it work:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = ${true}
I did this a couple of days ago and then my Invoke-RestMethod call started working.
I then rebooted my server and tried to execute the same again but now I get the following error:
PS C:\Users\Administrator> [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $True
Exception setting "ServerCertificateValidationCallback": "Cannot convert value "True" to type
"System.Net.Security.RemoteCertificateValidationCallback". Error: "Invalid cast from 'System.Boolean' to
'System.Net.Security.RemoteCertificateValidationCallback'.""
At line:1 char:1
+ [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Blockquote
I've tried setting my execution policy to unrestricted but I still have the same problem.
I'm running on Windows Server 2012 with PSVersion 3.0.
I've no idea why it worked before. I'm pretty sure there was nothing special i had done previously.
I would appreciate any suggestions!
I finally found a website that had the following command:
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Note that 'System.' is missing.
Running this command worked as expected.