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

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

Related

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

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.

Error creating Outlook Rules in Powershell

I've been messing around with Outlook objects for Powershell. I wanted to create Outlook Rules and I got it to work. Layed the project aside for a while.
When I picked it back up I was unable to create Outlook rules. Note that I did NOT change anything in the code.
Code generating Outlook Rule
$rules = $ol.Session.DefaultStore.GetRules()
$rule = $rules.Create("spamfilter",[Microsoft.Office.Interop.Outlook.OlRule Type]::olRuleReceive)
$condition = $rule.Conditions.SenderAddress
$condition.Enabled = $true
$condition.Address = #("<sender>")
$action = $rule.Actions.MoveToFolder
$action.Enabled = $true
[Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember("Folder",[System.Reflection.BindingFlags]::SetProperty,$null,$action,$junk)
$rules.Save()
The error
Exception calling "InvokeMember" with "5" argument(s): "Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))"
At line:22 char:5
[Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember("Folde ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : COMException
Someone please help me. What seems to be the issue? I'm lost, thanks.
I found the problem. I defined the $junk variable using Redemption outlook library.
This way it's not a legit MAPI object so it doesn't get recognized as a valid parameter.
Instead of defining $junk = $routlook.GetDefaultFolder(23), I had to use $junk = $ol.Session.DefaultStore.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderJunk)
But ofcourse I did not provide this information in the question because I didn't think it seemed relevant as nothing else in my script makes a problem of how this folder is defined.

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.