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

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

问题描述

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

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

$fileName = "c:\reports\1.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:\reports\error.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参数:Microsoft Office Excel无法访问文件C:\reports\1.xlsx。有几个可能的原因:
文件名或路径不存在
该文件正被另一个程序使用
您正在保存的工作簿与当前打开的工作簿名称相同。

Exception calling "Open" with "1" argument(s): "Microsoft Office Excel cannot access the file 'C:\reports\1.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,我得到了sam e结果。我已经验证在任务管理器中没有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:\reports\1.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:\reports\error.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.

我很失落任何想法?

推荐答案

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

I think you've hit a bug in Excel:


您必须创建一个文件夹(或64位窗口中的两个):

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

(32位,始终)

C:\Windows\System32\config\systemprofile\Desktop

C:\Windows\System32\config\systemprofile\Desktop

(64Bit)

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

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

我有同样的问题这是我唯一找到
的解决方案。

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

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

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

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