Abhimanyu
Abhimanyu

Reputation: 75

Browse and Submit to webform using Powershell

I have a webpage, which has a textbox and a Browse and Submit button. Is there a way where using PowerShell I can select a file and submit to the webpage?

Upvotes: 2

Views: 6497

Answers (1)

FoxDeploy
FoxDeploy

Reputation: 13537

When you click the Submit button on a site, the web browser performs an HTTP Post request.

PowerShell can perform HTTP Post request too, natively. Chrome and Edge also let you capture any request and convert it into PowerShell syntax. This can help you do what you want to do! Here's the steps you'll follow:

  1. Open Chrome
  2. Go to the website in question
  3. Hit F12 to open the developer tools
  4. Go to the network tab
  5. Click 'Preserve Log'
  6. Perform your action as you normally would to browse to a file and then click submit.
  7. You will see a new item appear in the Network tab in Chrome Devtools
  8. You can right-click and 'Copy as PowerShell' to extract the HTTP Request operation and modify it to suit your needs.

For example, Berkley has a PHP Upload example page from 1996 which is still online and still works! Let's use that to show these steps:

  • Browse to this site: http://cgi-lib.berkeley.edu/ex/fup.html
  • Open dev tools and check 'Preserve Log' enter image description here
  • Use the site as I normally do then click Submit... enter image description here
  • Right click and extract the completed PowerShell command by right-clicking the request -> Copy - > Copy as PowerShell enter image description here

Now I can use this as the basis of a script! Here's the PowerShell equivalent web request.

Invoke-WebRequest -Uri "http://cgi-lib.berkeley.edu/ex/fup.cgi" `
-Method "POST" `
-Headers @{
"Cache-Control"="max-age=0"
  "Upgrade-Insecure-Requests"="1"
  "Origin"="http://cgi-lib.berkeley.edu"
  "User-Agent"="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4170.0 Safari/537.36 Edg/85.0.552.1"
  "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  "Referer"="http://cgi-lib.berkeley.edu/ex/fup.html"
  "Accept-Encoding"="gzip, deflate"
  "Accept-Language"="en-US,en;q=0.9"
  "dnt"="1"
} `
-ContentType "multipart/form-data; boundary=----WebKitFormBoundaryyd1R4wTBVjKkPZWW"

I've got a guide to how to do this here for further reading.

Upvotes: 7

Related Questions