This topic contains 3 replies, has 4 voices, and was last updated by
April 18, 2014 at 2:23 pm #14633ParticipantPoints: 0Rank: 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.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
April 20, 2014 at 5:59 am #14649KeymasterPoints: 1,811Rank: 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.
April 22, 2014 at 5:41 am #14689MemberPoints: 0Rank: 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.
April 22, 2014 at 5:08 am #14688
The topic ‘Excel won't run in PS script from task scheduler’ is closed to new replies.