Share via


Download file in internet explorer - click save button

Question

Tuesday, November 14, 2017 8:07 PM

Hi pro's! Currently I'm trying to build a extraction tool for Exact Online (please don't be to critical on my code, it is my first powershell code). What I am trying to do is:
 1. Delete the old backup;
 2. Create a new backup;
 3. Download the backup (*.bak file) and
 4. Restore the backup in SQL Server.

All the steps are working for me. But the download script keeps failing because I'm running the Powershell-script from the Windows Task Scheduler. This script is running wheter a my account is logged on or not.

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$divisionURL,
[Parameter(Mandatory=$True,Position=2)]
[string]$AdministrationNumber,
[Parameter(Mandatory=$True,Position=3)]
[string]$username,
[Parameter(Mandatory=$True,Position=4)]
[string]$password
)

#Add Assembly's
Add-Type -Assembly "Microsoft.VisualBasic"

#Additional variables: do not edit
$submitButtonElementId = "LoginForm_btnSave"
$newFileName = "Backup" + $AdministrationNumber + ".bak"
$backupLocation = "E:\Extract Exact Online\" + $AdministrationNumber
$backupOldName = "E:\Extract Exact Online\"+ $AdministrationNumber + "\Backup.bak"

#Remove current Backup.bak from Downloads
Start-Sleep -Milliseconds 2000;
$FileName = "C:\Users\Downloads\Backup.bak"
if (Test-Path $FileName) {
  Remove-Item $FileName
  Start-Sleep -Milliseconds 4000;
}

#Remove current backup file
Start-Sleep -Milliseconds 2000;
$FileName2 = "E:\Extract Exact Online\"+ $AdministrationNumber + "\" + $newFileName
if (Test-Path $FileName2) {
  Remove-Item $FileName2
  Start-Sleep -Milliseconds 4000;
}

#Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
Set-ExecutionPolicy -ExecutionPolicy Bypass
Import-Module WASP

#Open internet explorer
$ie = New-Object -com internetexplorer.application;
$ie.navigate("https://start.exactonline.nl/docs/SysBackup.aspx?Type=D&Division=" + $divisionURL + "&BackupID=1&_Division_=" +$divisionURL); 
$ie.visible = $true;

While ($ie.Busy) { Sleep -m 10 }
    #Set internet explorer as first screen
    $ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
    [Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
    $wshell = New-Object -ComObject wscript.shell;

    #Webpage loading
    start-Sleep -s 20;

    #Log-in
    $ie.Document.getElementById("LoginForm_UserName").value = $username 
    $ie.Document.getElementById("LoginForm_Password").value = $password 
    $ie.Document.getElementById("LoginForm_btnSave").click()

    #Webpage loading
    start-Sleep -s 20;

    #Download ALT+S - Download
    $wshell.SendKeys('%{s}')  

    #Download time
    start-Sleep -s 90;

    #go to login scherm to log off
    $ie.navigate("https://start.exactonline.nl/docs/MenuPortal.aspx?_Division_=" + $divisionURL);

    #Webpage loading
    start-Sleep -s 20;

    #Log off
    $ie.Document.getElementById('LogOff').click()

    #Webpage loading
    start-Sleep -s 20;

    #Close Internet Explorer
    $ie.quit()

#Sleep
start-Sleep -s 20;

#Move backup to new file location
Move-Item $FileName $backupLocation
start-Sleep -s 3;

#Rename Backup.bak into new filename
Rename-Item -Path $backupOldName -NewName $newFileName
start-Sleep -s 3;

  

My problem is that the script does his job, but can not confirm the save button of internet explorer. That yellow bar at the bottom of my screen which is asking to save/open the file. In the script above I make internet explorer the first screen in a active session, and send a ALT+S combination to confirm the download. But when I'm not logged on on my PC, the script has no active browser to bring in the front of the screen, to confirm the download.

I do not have a direct link to the *.bak file. The download starts (when I'm logged on) if I visit a URL like: https://start.exactonline.nl/docs/Login.aspx?ReturnUrl=/docs/SysBackup.aspx?Type=D&Division=123456&BackupID=1&\_Division\_=123456 .

A Invoke-WebRequest (after logging in) like the code below returns me just the HTML code of the page (with a .bak extension).

Invoke-WebRequest -uri "https://start.exactonline.nl/docs/Login.aspx?ReturnUrl=/docs/SysBackup.aspx?Type=D&Division=123456&BackupID=1&_Division_=123456" -Outfile "C:\Downloads\Backup.bak"

How can I click on the save button in a inactive ineternet explorer session, or download the *.bak file? Or is it downloading a html page because I need a form log-in. Is there anny way maybe to click on the save button in internet exploren, in a inactive internet explorer session? I have searched the internet for a few days now, but did not find my problem a working solution yet. All tips are welcome, thanks in advance.

All replies (2)

Tuesday, November 14, 2017 8:17 PM ✅Answered | 1 vote

You cannot force a download of a file without answering the challenge. This is by design.   Some files can be downloaded using the WebClient.DownloadFile() method if you have a direct link to the file and not a triggered link.

Ask the vendor for a direct link or see if that have an API that allows remotely doing this.  Only the web vendor can tell you if thisi is possible and how it can be done if possible.

\(ツ)_/


Tuesday, November 14, 2017 8:29 PM

You cannot force a download of a file without answering the challenge. This is by design.   Some files can be downloaded using the WebClient.DownloadFile() method if you have a direct link to the file and not a triggered link.

Ask the vendor for a direct link or see if that have an API that allows remotely doing this.  Only the web vendor can tell you if thisi is possible and how it can be done if possible.

\(ツ)_/

Thanks for your reply, even though it is not the answer I had hoped for. Problem is that this was the only way to download the database backup file. I will try to contact the vendor or look for a way to keep my computer logged on in a remote desktop (to let script always run with active internet explorer sessions).