Upload powershell file to asp.net

I have an asp.net mvc application that allows you to load a company structure using a CSV file. I was asked about the possibility of automating this function using a powershell script. Creating CSV in powershell is easy, but I have no idea how to upload to asp.net.

My first choice was to use WebClient, but I have an authentication problem. In mvc we use authentication. I read here that it is possible, but when changing my login form I will have to send an updated script to the client. I would like to omit the client side mange code.

The second option is to split a separate controller and use an authorization token in it, but I look like “reinvent the wheel again” because I will need to write all the code responsible for authentication.

Is it possible to improve one of the above options? Or maybe there is a better choice?

+7
source share
3 answers

You may be able to use your existing web service using the Invoke-RestMethod , but it can take two separate calls. For both calls, you probably need to say -Method Post . It can be done using WebClient, but the cmdlet may be easier to describe. I didn't actually try 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 call, you specify the URL of the login page and simulate logging into web forms by specifying the username and password in parmeter -Body and use -SessionVariable to capture and store the context to create the next authenticated request.

In the second request, you use the data load URL, the -WebSession parameter to provide the context set by the first request, and -Body to load the CSV. Note that you need the dollar sign in the webSession variable in the second, but not the first.

Of course, you will need to save the username / password for automation in order to use it somewhere, but it is always necessary for automatic automation. A possible approach to mitigate this might be to use credentials based on a client certificate, and not for web form authentication.

+1
source

How about using a side channel?

There are two approaches. You either send it to the client’s web server from time to time, or the web server downloads data from you.

To send FTP data over SSL, it must be secure enough. The following is an example on how to manage FTP using Powershell. FTP / SSL is easy enough to configure for IIS.

To get the data, just publish the CSV on your own website. Set up a script on a client web server that loads CSV from time to time. If the CSV should not be accessible to anyone other than the client, a client certificate is required.

0
source

I would do it like this:

  • Step 1. Create an https webpage on asp.net server to receive csv
  • Step 2. Create a powershell script file that calls curl with the -F option to publish the file to it and add any metadata you need when calling
  • Step 3: After receiving the file, save it using the metadata provided on the form and add clientid / date / etc to the file
0
source

All Articles