0

I need to upload a file to a web server (to a Jira server, actually). The below code works perfectly when the script is run in PowerShell ISE:

$WebClient = new-object System.Net.WebClient
$WebClient.Headers.Add("Authorization", "Basic " + $AuthStr)
$WebClient.Headers.Add("X-Atlassian-Token", "nocheck")
$WebClient.UploadFile($BasePath + "/attachments", $AttachmentFile) >$null

However, the same code with the same data throws an error when executed in a usual PowerShell console window:

Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At C:\Temp\Qualys\ProcessWeeklyReport.ps1:69 char:5
+     $WebClient.UploadFile($BasePath + "/attachments", $AttachmentFile ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

I was unable to obtain a more detailed error message. No PowerShell profiles have been created, the Powershell environment is in its default state.

Replacing the plain string with [system.uri] type variable doesn't change the situation.

This is a local issue, not an error returned by the web server. Packet sniffer doesn't show any communications to the server if the error is thrown.

Can someone suggest where to dig?

Evgeny
  • 43
  • 1
  • 8
  • See if `$Error[0].Exception.InnerException` contains a more specific error message. – mklement0 Oct 22 '19 at 19:37
  • 1
    As a test, restart the ISE and see if it still works - you might have some variables from a previous iteration of your script still live in memory. – mclayton Oct 22 '19 at 21:40
  • @mklement0 Unfortunately, that variable contains only what is written in the error output: "An exception occurred during a WebClient request." – Evgeny Oct 24 '19 at 14:50
  • @mclayton ISE restarted, no changes. In any case, there is nothing in the code that could remain from the past, The object is created from scratch, there are only three external variables (dynamically formed text strings). – Evgeny Oct 24 '19 at 14:52
  • I see. Just to rule out basic issue: The ISE and your console window are running the same PowerShell version and edition (Windows PowerShell vs. PowerShell Core)? – mklement0 Oct 24 '19 at 14:53
  • @mklement0 Yes, that's the same server. Win2k16, default components, PS version 5.1.14393.3053. I have animpression that some global variable can have different values in ISE and the console. However, no PS profiles are defined, co I can't even imagine where from that variable can come. – Evgeny Oct 24 '19 at 16:11

1 Answers1

0

Found it. $AttachmentFile string variable contains file name. However, this is only the name of file without any path. This file is located in the script folder. Due to some reason UploadFile method can't find this file if executed in the standalone console. The full path to the file (including folder path) should be specified to make it work properly.

The above code should be modified to work in both in ISE and the console:

$WebClient = new-object System.Net.WebClient
$WebClient.Headers.Add("Authorization", "Basic " + $AuthStr)
$WebClient.Headers.Add("X-Atlassian-Token", "nocheck")
$WebClient.UploadFile($BasePath + "/attachments", (Get-Location).Path + "\" + $AttachmentFile)

BTW this error (file not found) can be seen if analyzing $Error[0] content within try-catch block. The place where the system tries to find this file is the root of user profile (c:\users\username). This information is absent if $Error[0] is displayed from command line.

Evgeny
  • 43
  • 1
  • 8
  • 1
    This may be related: https://stackoverflow.com/questions/11246068/why-dont-net-objects-in-powershell-use-the-current-directory – js2010 Oct 24 '19 at 19:27
  • @js2010 Possibly so. But it doesn't explain why the file search root is different in ISE and PS console. As System.Net.WebClient is a .NET Framework class, seems ISE and console initialize .NET Framework in slightly different ways. – Evgeny Oct 25 '19 at 13:38
  • 1
    It's possible that you changed directories in powershell or cmd before running the .net method. I'm just covering all the bases. – js2010 Oct 25 '19 at 14:52