-1

Please, I need help with uploading excel files by Powershell. I need to write script, that POST existing excel (well-formated according external system rules) to URI of that system. Script need to be authentificate via NTLM (on Apache) and then post data.

I have tried something like this:

Invoke-WebRequest -Uri $uri -Method Post -InFile $filePath -ContentType "multipart/form-data" -Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))} 

where $uri is something like http://[servername]/[applicationame]/apix/marketdata/upload

and $filePath is C:\Users..\Documents\Market_data.xlsx

Error:

Invoke-WebRequest : {"status":"ERROR","message":"HTTP 400 Bad Request","additionalMessages":[]}
At C:\Users\..\script.ps1:48 char:1
+ Invoke-WebRequest -Uri $uri -Method Post -InFile $filePath -ContentTy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

This Curl.exe command is working, so I need to retype it to PS curl

curl.exe -X POST -H "Pragma: no-cache" -H "Origin: http://[server]" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Accept: */*" -H "Cache-Control: no-cache" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" -H "Referer: http://[server]/" -F "Content-Type=application/vnd.ms-excel" -F "File=@$filePath" -F "filename=$filePath" "http://[servername]/[applicationname]/apix/marketdata/upload?User=[DOMAIN]%255C[USER]" --ntlm --negotiate -u 'DOMAIN\user:password'

1 Answers1

0

Ok so I don't have the ability to test right now but this should be possible using the -credential parameter and passing in whatever credential object you need to be using.

Mike Garuccio
  • 2,588
  • 1
  • 11
  • 20
  • I have tried this way: Invoke-WebRequest -Uri $uri -Method Post -InFile $file_to_upload -ContentType "application/vnd.ms-excel" -Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("[user]:[password]" ))} but I got "HTTP 415 Unsupported Media Type" – Martin Fedy Fedorko Nov 03 '16 at 16:12
  • ok, unless I am missing something the code you just posed does not include the -credential parameter or a credential object, what happens if you try including them? – Mike Garuccio Nov 03 '16 at 21:20