Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Excel won't run in PS script from task scheduler

$
0
0

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.DisplayAlerts=$true
    $xl.Visible =$false
    $Workbook = $xl.workbooks.open((Join-Path $dir "ExportCodesBackup.xls”),0,$True)
    $Workbook.SaveAs("$dir\ExportCodes.csv",6)
    $Workbook.Saved = $True
    $xl.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | Out-Null
    

    Viewing all articles
    Browse latest Browse all 13067

    Latest Images

    Trending Articles



    Latest Images