Reputation: 75
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
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:
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:
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