Upload file form powershell to asp.net Upload file form powershell to asp.net powershell powershell

Upload file form powershell to asp.net


You might be able to use the existing web service using the Invoke-RestMethod cmdlet, but it could take two separate invokes. For both invocations you'll probably need to say -Method Post. Its possible to do all this with WebClient, but the cmdlet may be easier to describe. I haven't actually tried this, but it could look something like this:

Invoke-RestMethod -Method Post $loginPage -SessionVariable webSession -Body "..."Invoke-RestMethod -Method Post $uploadPage -WebSession $webSession -Body "..."

For the first invocation, you specify the URL of the login page, and would simulate a web forms login by providing a username and password in the -Body parmeter, and use -SessionVariable to capture and store context for making the next request(s) authenticated.

In the second request, you use your data upload URL, the -WebSession parameter to supply the context established by the first request, and -Body to upload your CSV. Note that you need the dollar sign on the webSession variable in the second one, but not the first.

Of course, you'll need to store the username/password for the automation to use somewhere, but that's always needed for unattended automation. A possible slick approach to mitigate this would be to use client certificate-based credentials rather than a web form authentication.


How about using a side channel?

There are two approaches. You either send it to the customer's web server every now and then, or the web server downloads the data from you.

For sending data, FTP over SSL should be secure enough. Here is an example about how to command FTP with Powershell. FTP/SSL is easy enough to configure for IIS.

For receiving data, just publish the CSV on your own web site. Set up a script on the customer's web server that downloads CSV every now and then. If the CSV should not be accessible to anyone but the customer, require a client certificate.


I would probably do it like this:

  • Step 1: Create an https webpage on the asp.net server to receive the csv
  • Step 2: Create a powershell script that calls curl with the -F option to post a file to it and append any metadata you need on the call
  • Step 3: Upon receiving the file, store it using the metadata provided in the form and append clientid/date/etc to the file