WebClient HTTP upload - "An exception occurred during a WebClient request." - powershell

Hoping someone can offer some advice with this - it may be that I've been staring at my screen too long so apologies if I'm missing the obvious!
Within a script I'm trying to add an upload/download speedtest to my webserver, but I'm having an issue trying to upload a file to my webserver via HTTP(s) in Powershell. I can get download working no problem.
The upload snippet of my script is as follows:
$uploadPath = "https://www.domain.co.uk/speedtest/up/"
$localfile = "C:\Users\user\Downloads\100MB.bin"
$wc = New-Object System.Net.WebClient
$wc.UploadFile($localfile, $uploadPath)
I've tried over HTTP & HTTPS, file permissions are currently 777 on the "up" folder on the server (just as a test) and I've also tried $uploadPath as:
"https://www.domain.co.uk/speedtest/up/100MB.bin".
The following is the error received when running the upload section of the script:
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At C:\Users\User\desktop\uploadtest.ps1:4 char:1
+ $wc.UploadFile($localfile, $uploadPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
(file paths etc are just temporary for this test too)
Any suggestions you can make would be appreciated! I'd ideally like to use the webclient method (if possible) as the download uses the same method.

Related

Exception calling "ExtractToFile" when unzipping

I've tried a couple of solutions to unzip a file containing the path:
\Content\F_C\Jenkinsworkspace\workspace\BUILD.PROJECT.GENERICS\PROJECT_Generics\PROJECT.Generics\PROJECT.Generic\obj\Debug\Package\PackageTmp
but I get the error:
Exception calling "ExtractToFile" with "3" argument(s): "Could not find a part of the path
Expand-Archive -LiteralPath F:\Jenkinsworkspace\workspace\BUILD.GENERIC.GENERICS\GENERIC.Generic.zip -DestinationPath F:\Jenkinsworkspace\workspace\BUILD.GENERIC.GENERICS\UNZIPED2 -Force
Exception calling "ExtractToFile" with "3" argument(s): "Could not find a part of
the path 'F:\Jenkinsworkspace\workspace\BUILD.GENERIC.GENERICS\UNZIPED2\Content\
F_C\Jenkinsworkspace\workspace\BUILD.GENERIC.GENERICS\GENERIC_Generics
\GENERIC.Generics\GENERIC.Generic\obj\Debug\Package\PackageTmp\modules\
_protected\EPiServer.GoogleAnalytics\EPiServer.GoogleAnalytics.zip'."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:1033 char:25
+ ... [System.IO.Compression.ZipFileExtensions]::ExtractToFile( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DirectoryNotFoundException
I'm guessing it's because I have a zip within a zip.
In our case, though the path was short, we were getting the same error.
Sharing our solution which might be useful for someone.
We were trying to unzip the package on our application server through powershell script. Basically powershell script runs on our build server & remotely connect to our application server. It was then trying to unzip the package present on application server.
The above scenario was working fine for quite some time before it starts to throw the error during unzipping.
Solution:
We found the reason was anti virus policy, which was blocking the unzipping through remote powershell script.
Soved using this Blog :
https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/
changing the group policy solved the problem

powershell System.Net.WebClient.DownloadFile(String, String) throws cryptic errors

This is my first ever question on stack exchange, so I ask forbearance in the face of breaking community etiquette. If I can clarify any part of my question, I would be more than happy to do so for you.
I'm learning powerShell for my internship, and currently I'm working on a script that pulls a .csv file from a sharepoint site. My aim is to take the file downloaded, do stuff, and then put it back on the site. Before I can do the other things, I'm trying to download the file, and my root issue lies in .net.webclient's .downloadfile() method. At execution I keep seeing a vague exception that I can't find information for anywhere on the web or among my colleagues. Below I will include the snippet involved, and the error message. Below that I will give you an Idea of troubleshooting steps I've taken.
--------code
## Download summary file from Sharepoint downloads new serverlist
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = $spCreds ## defined earlier with import-clixml - also tried get-credential
$webclient.DownloadFile($URIaddress, $DownloadPath) ## defined earlier: $DownloadPath = "c:\temp\Summary.csv"
--------exception
Exception calling "DownloadFile" with "2" argument(s): "An exception
occurred during a WebClient request."
At line:13 char:5
+ $webclient.DownloadFile($URIaddress, $DownloadPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
--------action taken
The credentials I provide should be valid because I am able to download the contents of $URIaddress when I enter the URI into my browser.
I've been able to coax .downloadFileAsync(string, string) into creating a file at the specified $downloadPath, but the download never seems to commence, and the file stays empty. When try to .DownloadFile() into a random variable ($foo), $foo | gm reveals to be null.
If any of you have ideas I'd love to give them a shot!
Indeed quite cryptic.
Have a look at the exception and the inner exception these might provide you with more insights on what happened:
$Exception = $error[0].Exception
$Exception.InnerException

Strange Error with Powershell when downloading files

I got a very strange error when trying to install dnvm on powershell. Actually I get this error with any attempt to download anything, e.g. "wget" produces a similar error.
I thought first that I don't have the rights to execute but running this in an Administrator shell + I changed the Security Policy.
Then I figured that the PS doesn't have access to the internet, so I adjusted the Firewall and even added PS to the allowed programs. No luck!
Now the weird thing is that I can download files just fine via IE.
This is the error that I get
Exception calling "DownloadString" with "1" argument(s): "An exception occurred during a WebClient request."
At line:1 char:17
+ &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.gith ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Anyone as an idea?
Also this is a machine running on AWS.
This is a shot in the dark, but first thing that comes to mind is proxy settings.
System.Net.WebClient does not automatically pick up IE's proxy configuration, but you should be able to force it to do so with:
$WebClient = New-Object System.Net.WebClient
$WebProxy = [System.Net.WebProxy]::GetDefaultProxy()
$WebClient.Proxy = $WebProxy
$WebClient.DownloadString($url)
or with your very compact syntax:
&{$Branch='dev';($w=New-Object Net.WebClient).Proxy=[Net.WebProxy]::GetDefaultProxy();iex $w.DownloadString($url)}

Exception creating client using AWS SDK

I am currently using the following sequence of commands in a Windows 7 PowerShell window.
Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.dll"
$secretKeyID="ABCDEFGHIJKLMNOPQRS"
$secretAccessKeyID="LONGKEYBLAHBLAHBLAHBLAHBLABLAH"
$AWSclient=[Amazon.AWSClientFactory]::CreateAmazonS3Client($secretKeyID,$secretAccessKeyID)
However, I get this exception below. Please let me know what Im doing wrong. This is what I see most people using successfully.
Exception calling "CreateAmazonS3Client" with "2" argument(s): "No RegionEndpoint or ServiceURL configured"
At line:1 char:1
+ $oAWSclient=[Amazon.AWSClientFactory]::CreateAmazonS3Client($secretKeyID,$secret ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : AmazonClientException
One thing that I think is a problem is that my bucket is not checked for 'enable website hosting'. Does this need to be set to true? I just want to upload a file to the bucked via powershell script.
Duh... I realized that I need to use the AWS Powershell because it has all the settings built into it. Im not exactly sure how to properly refer to this version of powershell, but it is available after installing the AWS SDK.
Since the error specifically states that you're missing the RegionEndpoint or ServiceURL, you could also just include one of them. Since you're connecting to S3, all you need is to include the RegionEndpoint.
Add-Type -Path "C:\Program Files (x86)\AWS SDK for .NET\bin\Net35\AWSSDK.dll"
$secretKeyID="ABCDEFGHIJKLMNOPQRS"
$secretAccessKeyID="LONGKEYBLAHBLAHBLAHBLAHBLABLAH"
$client = [Amazon.AWSClientFactory]::CreateAmazonS3Client($secretKeyID, $secretAccessKeyID, [Amazon.RegionEndpoint]::USEast1)
(Replace USEast1 with the value configured with your S3 Bucket)

"Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."

I am trying to setup to download need files to automate and install process. I keep getting errors ever time I run the script and I have changed it seven ways from sunday and it still gives me errors.
The script is:
if (test-path $java_path)
{
Write-Output "Java already installed. Skipping script"
exit 0
}
else
{
$source = "http://our.server.com/java-installer.zip"
$destination = "c:\CHPACS"
$client = new-object System.Net.WebClient
$client.DownloadFile($source, $destination)
}
The error message that I am getting is
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At C:\ps_script\testjava.ps1:41 char:31
+ $client.DownloadFile <<<< ($source, $destination)
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DotNetMethodException
Do I need to create a function to make this work properly?
Thank you:
If you look at the MSDN documentation for the DownloadFile method, you'll notice that the second parameter is a filename, not a directory. So, if you re-define $destination to something like:
$destination = "c:\CHPACS\java-installer.zip"
then it should work.
Check secondly that the file that you're trying to download isn't open or being executed at the moment. This exception will be raised if the file is in use.
You should give a file name as David said + the folder should exist.
In my case the URL wasn't reachable on the machine I was trying to execute the script. Had to enable proxy rules to scope it to the machine to download the file.