Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Excel Automation - Save/Open fails in Scheduled Task

I created a simple powershell script that will create an excel instance and save a workbook:

$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open("C:\Test\foo.xls")
$workbook.SaveAs("C:\Test\bar.xls")

# cleanup code ...

When I run this from powershell directly it works fine. I created a scheduled task that runs it, and when I have the option set that will "Run only when the user is logged in" then it will run fine.

When I change this option to run whether the user is logged in or not, it will fail trying to open/save the file. The account I am using has the correct permissions. I have the account set up to Log in as a service.

Any suggestions?

like image 465
Dismissile Avatar asked Dec 12 '22 10:12

Dismissile


1 Answers

I've been burned by this and didn't want to rewrite the code. I saw your post and several others which made me about to give up. However, my persistence paid off. I was trying to have Jenkins run a script to inventory our production environment and output to Excel. I didnt want a text doc because I was highlighting software versions that didnt match in RED, so needed Excel.

Here is the answer that worked for me:

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Desktop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

Link that someone provided as the source:

http://www.patton-tech.com/2012/05/printing-from-scheduled-task-as.html

My source was:

http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/aede572b-4c1f-4729-bc9d-899fed5fad02

like image 87
JoeB Avatar answered Dec 28 '22 09:12

JoeB