Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell and Excel Issue when Automating

I am facing a curious problem.

When I ran this code :

$Excel = New-Object -Com Excel.Application 
$book = $Excel.Workbooks.Add()
[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
sleep 10
$book.saveas("c:\Temp\test.xlsx")
$Excel.quit()
Stop-Process -Name "Excel*"

in PowerGui, POwerSHell host..anywhere, it works beatiful.

If I put into a .bat and calls powerhsell "c:\temp\excel.ps1" works fine too.

But If i try to schedule this guy into schedule task or run by a sql server job type cmd the error happens :

Message Exception calling "SaveAs" with "1" argument(s): "SaveAs method of Workbook cla ss failed"

Only when It is called by SQL Server Jobs ( that is a simple cmd command too and the line is the same as the .bat powerhsell "c:\temp\excel.ps1" ) the error happens.

What it can be ? I tried change the fileformat..etc..and nothing

Thanks Guys :)

like image 232
Laerte Junior Avatar asked Jun 13 '12 02:06

Laerte Junior


1 Answers

The Solution is :

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

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

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

The solution is in the Forum :

Solution Forum MS

like image 68
Laerte Junior Avatar answered Sep 22 '22 20:09

Laerte Junior