Skip to content

Instantly share code, notes, and snippets.

@airtank20
Created January 7, 2018 20:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save airtank20/740a7a1cf98ce423a5e52ef199c877da to your computer and use it in GitHub Desktop.
Save airtank20/740a7a1cf98ce423a5e52ef199c877da to your computer and use it in GitHub Desktop.
Script to download file from javascript Postback
#URL that needs to be fetched
$url = "https://site.state.gov/default.aspx"
#get the server name in case the process jumps to another script
$serverName = $env:computername
#wrapped it into a try/catch for some type of error handling
TRY {
#use invoke-webrequest to fetch a session from the site
Invoke-WebRequest $url -SessionVariable session -UseBasicParsing
#add a site using the session information from the above web request
$addUserSite = Invoke-WebRequest $url -WebSession $session
$addUserForm = $addUserSite.Forms[0] #Invoke-WebRequest does a lot of auto processing.
#add form fields for the event target & argument that is needed to actually do the post back
$addUserForm.Fields["__EVENTTARGET"] = "dnn`$abcd1234`$File`$ExcelFile"
$addUserForm.Fields["__EVENTARGUMENT"] = ""
#where are we saving the file & what is file name
$filename = "C:\temp\Download_FIle_Name.txt"
#invoke another web request using the same url, session information, and out put to the $filename variable
Invoke-WebRequest -uri $url -method post -Body $addUserForm.Fields -WebSession $session -UseBasicParsing -Outfile $fileName
}
# catch any errors from above and send an email to the right people
CATCH {
Send-MailMessage -To "it@somecompany.com" -From "donotrely@somecompany.com" -Subject "Some important thing just happened" -SmtpServer "server.smtp.com" -Body "Check stuff out on $serverName"
}
@nicolaibaralmueller
Copy link

Hi,

I am actually attempting to download a file from a site that uses javascript download button via Invoke-WebRequest.

Can you explain the parameters in event target which seem a bit random? The variables are not set in your code.

$addUserForm.Fields["__EVENTTARGET"] = "dnn$abcd1234$File$ExcelFile" `

@airtank20
Copy link
Author

The parameters are obtained by looking at the initial link that does the post back. That was written to handle the post back of the javascript and they have to be set in order for the powershell to work correctly.

@nicolaibaralmueller
Copy link

The parameters are obtained by looking at the initial link that does the post back. That was written to handle the post back of the javascript and they have to be set in order for the powershell to work correctly.

How does one find the link that does the post back? I can't find any relevant information looking at the page in developer mode.

@airtank20
Copy link
Author

In my case, I was trying to do this post back where DNN was the provider. I was able to hover over the initial link to identify what the parameters had to be set to. I believe if you look at the HTML of the form/page itself there's a form the page that will identify the parameters as well.

@jimbrig
Copy link

jimbrig commented Sep 30, 2023

I do not understand the same as @nicolaibaralmueller - I am attempting to get the excel file from this site: https://apps.dnr.wi.gov/pheasantstocking/Index.aspx

@airtank20
Copy link
Author

@jimbrig The event target would be "lnkExport" I believe. I have not tested it.

2023-10-02_09-06-09

@jimbrig
Copy link

jimbrig commented Oct 9, 2023

Yeah I eventually got it working - not an ideal scenario though especially because I wanted to allow it to be integrated into PowerQuery which is not possible without scripting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment