作为计划任务运行时,Powershell 脚本无法访问文件 [英] Powershell script cannot access a file when run as a Scheduled Task

查看:45
本文介绍了作为计划任务运行时,Powershell 脚本无法访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Powershell (2.0) 脚本有以下代码片段:

My Powershell (2.0) script has the following code snippet:

$fileName = "c:
eports1.xlsx"
$xl = new-object -comobject excel.application
$xlFormat = [Microsoft.Office.Interop.excel.XlFileFormat]::xlWorkbookDefault
$xl.displayalerts = $false
$workbook = $xl.workbooks.open($fileName)
#Code to manipulate a worksheet
$workbook.SaveAs($fileName, $xlformat)
$xl.quit()
$error | out-file c:
eportserror.txt

我可以毫无问题地在 Powershell 命令提示符下运行此脚本.电子表格得到更新,error.txt 为空.但是,当我在任务计划程序中将其作为任务运行时,第一行出现错误.

I can run this script in the Powershell command prompt with no issues. The spreadsheet gets updated, and error.txt is empty. However, when I run it as a task in Task Scheduler, I get errors with the first line.

使用1"参数调用Open"时出现异常:Microsoft Office Excel 无法访问文件 'C: eports1.xlsx'.可能的原因有多种:文件名或路径不存在.该文件正被另一个程序使用.您尝试保存的工作簿与当前打开的工作簿同名.

Exception calling "Open" with "1" argument(s): "Microsoft Office Excel cannot access the file 'C: eports1.xlsx'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.

我使用与在 Powershell 命令提示符中运行脚本相同的凭据运行任务.当我手动运行脚本时,它可以毫无问题地打开、更新和保存电子表格.当我在任务计划程序中运行它时,它无法访问电子表格.

I run the task with the same credentials I use to run the script in the Powershell command prompt. When I run the script manually, it can open, update, and save the spreadsheet with no issues. When I run it in Task Scheduler, it can't access the spreadsheet.

相关文件对所有用户都是可读/可写的.我已经验证我可以使用相同的凭据在 Excel 中打开文件.如果我制作一个新的电子表格并将其名称作为 $filename 输入,我会得到相同的结果.我已经验证任务管理器中没有 Excel.exe 的实例.

The file in question is readable/writeable for all users. I've verified I can open the file in Excel with the same credentials. If I make a new spreadsheet and put its name in as the $filename, I get the same results. I've verified that there are no instances of Excel.exe in Task Manager.

奇怪的是,如果我使用 get-content,我没有任何问题.另外,如果我制作一个新的电子表格,我没有任何问题.

Oddly, if I use get-content, I don't have any problems. Also, if I make a new spreadsheet, I don't have any problem.

$fileName = "c:
eports1.xlsx"
$xl = get-content $spreadsheet
$xl = new-object -comobject excel.application
$xlFormat = [Microsoft.Office.Interop.excel.XlFileFormat]::xlWorkbookDefault
$xl.displayalerts = $false
# Commented out $workbook = $xl.workbooks.open($fileName)
$workbook = $xl.workbooks.add()
#Code to manipulate a worksheet
$workbook.SaveAs($fileName, $xlformat)
$xl.quit()
$error | out-file c:
eportserror.txt

效果很好.所以 Get-ChildItem 可以毫无问题地打开文件.如果我手动运行 ComObject 可以打开文件,但如果它作为任务运行则不能.

That works fine. So Get-ChildItem can open the file with no issue. ComObject can open the file if I run it manually, but not if it's run as task.

我很茫然.有什么想法吗?

I'm at a loss. Any ideas?

推荐答案

我认为你在 Excel 中遇到了错误:

I think you've hit a bug in Excel:

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

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

(32 位,始终)

C:WindowsSystem32configsystemprofileDesktop

C:WindowsSystem32configsystemprofileDesktop

(64位)

C:WindowsSysWOW64configsystemprofileDesktop

C:WindowsSysWOW64configsystemprofileDesktop

我遇到了同样的问题,这是我唯一的解决方案找到了.

I have had the same problem and this was the only solution i have found.

来自 TechNet 论坛(通过 PowerShell 和 Excel 自动化时的问题)

这篇关于作为计划任务运行时,Powershell 脚本无法访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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