Created
January 7, 2018 20:09
-
-
Save airtank20/740a7a1cf98ce423a5e52ef199c877da to your computer and use it in GitHub Desktop.
Script to download file from javascript Postback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" | |
} |
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
@jimbrig The event target would be "lnkExport" I believe. I have not tested it.
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
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.