5

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 :)

Community
  • 1
  • 1
Laerte Junior
  • 244
  • 3
  • 16
  • Are the filepaths as per your example are they different in the real case? Are the tests on the same machine? Can you try and run the sql job as your credentials and test it? – Preet Sangha Jun 13 '12 at 02:13
  • Yes..in the same machine. Only not works when it runs sheduled. I think it is something with culture info, but I am changing the culture info too. Really dont know – Laerte Junior Jun 13 '12 at 02:17
  • Also the SQl Agent Account I put as domain admin to discartd security too. Juts not works – Laerte Junior Jun 13 '12 at 02:27
  • Add a try catch as log the exception if any. See this http://stackoverflow.com/questions/2182666/powershell-2-0-try-catch-how-to-access-the-exception – Preet Sangha Jun 13 '12 at 02:30
  • That is the exception. Exception calling "SaveAs" with "1" argument(s): "SaveAs method of Workbook cla ss failed" + $book.saveas <<<< ("c:\Temp\test.xlsx") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation – Laerte Junior Jun 13 '12 at 02:39
  • Just Tried in the Windows Scheduler Task and the same error happens. run it by domain admin account. – Laerte Junior Jun 13 '12 at 02:51
  • Have a look at this : http://stillpearling.blogspot.co.nz/2009/02/more-powershell-adventures.html – Preet Sangha Jun 13 '12 at 03:02
  • 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 http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/aede572b-4c1f-4729-bc9d-899fed5fad02 Thanks A LOT – Laerte Junior Jun 13 '12 at 03:16
  • @lj - please put the solution in an answer for other users. PS well done! – Preet Sangha Jun 13 '12 at 03:18

1 Answers1

7

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

Laerte Junior
  • 244
  • 3
  • 16