使用Powershell

时间:2017-11-09 20:55:42

标签: powershell internet-explorer

目前我正在尝试为Exact Online构建一个提取工具。我想做的是:

  1. 删除旧备份;
  2. 创建新备份;
  3. 下载备份(* .bak文件)和
  4. 在SQL Server中还原备份。
  5. 所有步骤对我有用。但下载脚本仍然失败,因为我正在从Windows任务计划程序运行Powershell脚本。我的帐户登录与否,此脚本正在运行。

    [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;
    

    我的问题是脚本完成了他的工作,但无法确认Internet Explorer saveprompt的保存按钮。我将Internet Explorer设置为活动会话中的第一个屏幕,并发送ALT + S组合以确认下载。但是当我没有登录时,脚本没有活动的浏览器来引入前端来下载文件。

    我没有直接链接到* .bak文件。如果我访问以下网址,则会开始下载(当我登录时):https://start.exactonline.nl/docs/Login.aspx?ReturnUrl=/docs/SysBackup.aspx?Type=D&Division=123456&BackupID=1&Division=123456

    一个Invoke-WebRequest(登录后)就像下面的代码一样,只返回页面的HTML代码(扩展名为.bak)。

    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"
    

    如何在非活动的ineternet资源管理器会话中单击“保存”按钮,或下载* .bak文件?

0 个答案:

没有答案