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

9
  • 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? Commented Jun 13, 2012 at 2: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 Commented Jun 13, 2012 at 2:17
  • Also the SQl Agent Account I put as domain admin to discartd security too. Juts not works Commented Jun 13, 2012 at 2:27
  • Add a try catch as log the exception if any. See this stackoverflow.com/questions/2182666/… Commented Jun 13, 2012 at 2: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 Commented Jun 13, 2012 at 2:39

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.