PowerShell和Excel自动化时出现问题 [英] PowerShell and Excel Issue when Automating

查看:173
本文介绍了PowerShell和Excel自动化时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我运行这段代码时:

 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()
停止进程-NameExcel在PowerGui,POwerSHell主机中的

。其中,它的工作很好。



如果我放入.bat并调用powerhsellc:\temp\excel.ps1也可以正常工作。



但是,如果我尝试安排这个人进入计划任务或运行一个sql服务器作业类型cmd错误发生:



消息
异常调用SaveAs与1参数:SaveAs方法的工作簿cla
ss失败



只有当SQL Server作业调用(这是一个简单的cmd命令也与.bat powerhsellc:\temp\excel.ps1)的行相同e错误发生。



可以做什么?我试过改变fileformat..etc..and没有什么



感谢贵司:)

解决方案

解决方案是:



您必须创建一个文件夹(或64位Windows上的两个):



(32Bit,always)



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



(64位)



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



解决方案在论坛中:



解决方案论坛MS


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

解决方案

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

这篇关于PowerShell和Excel自动化时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆