Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

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