Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to download file from javascript Postback
#URL that needs to be fetched
$url = ""
#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
#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
Send-MailMessage -To "" -From "" -Subject "Some important thing just happened" -SmtpServer "" -Body "Check stuff out on $serverName"
Copy link

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.

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