Excel won't run in PS script from task scheduler

Welcome Forums General PowerShell Q&A Excel won't run in PS script from task scheduler

This topic contains 3 replies, has 4 voices, and was last updated by

4 years, 11 months ago.

  • Author
  • #14633

    Points: 0
    Rank: Member

    I have a fairly simple two part task that I want to run daily via the task scheduler:

  • Make a backup of a spreadsheet
  • Open the backup copy in Excel and save it as a CSV file
  • The script below runs fine interactively, however, Excel never launches when run from the task scheduler. It's as if PowerShell cannot fine the Excel COM object. Excel was installed on this server specifically for this task. It is PS v4 on Windows Server 2008 R2.

    $dir = "D:\foo2_sys\ExportCodes"
    # Make backup copy of spreadsheet
    $lwt = (Copy-Item -Path (Join-Path $dir "EXPORT CODES 2012.xls") `
        -Destination (Join-Path $dir "ExportCodesBackup.xls") `
        -Force -PassThru -ErrorAction Stop).LastWriteTime
    "Job run on         $(Get-Date)
    Codes last updated $lwt" |
        Out-File -FilePath (Join-Path $dir "timestamp.txt") -Encoding ascii
    Remove-Item -Path $dir\ExportCodes.csv -ErrorAction SilentlyContinue
    # Convert to CSV for import into shipping programs.
    $xl = new-object -comobject excel.application
    $xl.Visible =$false
    $Workbook = $xl.workbooks.open((Join-Path $dir "ExportCodesBackup.xls”),0,$True)
    $Workbook.Saved = $True
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
  • #14649

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Scheduled Tasks often have difficulty launching GUI apps that expect desktop interaction. This isn't really a PowerShell thing, unfortunately, it's an interaction between Excel and the Task Scheduler and the way they both behave.

    One of the many, many reasons I hate storing data in Excel.

  • #14689

    Points: 0
    Rank: Member

    Try creating this folder: "C:\Windows\System32\config\systemprofile\Desktop" (and "C:\Windows\SysWOW64\config\systemprofile\Desktop", if you're on a 64-bit OS and are launching a 32-bit version of Excel.) Sometimes this allows Excel to function from a service or scheduled task, though I couldn't really tell you why. See http://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64?forum=innovateonoffice for the thread where I originally discovered this trick.

    Note that Microsoft doesn't support using Office in this fashion.

  • #14688

    Points: 0
    Rank: Member


    See this thread (my suggestion regarding different topic, however my suggestion applies here I think):




  • The topic ‘Excel won't run in PS script from task scheduler’ is closed to new replies.

    denizli escort samsun escort muğla escort ataşehir escort kuşadası escort