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"
}
@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