简体   繁体   English

Powershell 脚本作为计划任务运行时无法访问文件

[英]Powershell script cannot access a file when run as a Scheduled Task

My Powershell (2.0) script has the following code snippet:我的 Powershell (2.0) 脚本有以下代码片段:

$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

I can run this script in the Powershell command prompt with no issues.我可以在 Powershell 命令提示符下运行此脚本,没有任何问题。 The spreadsheet gets updated, and error.txt is empty.电子表格得到更新,error.txt 为空。 However, when I run it as a task in Task Scheduler, I get errors with the first line.但是,当我在任务计划程序中将其作为任务运行时,第一行出现错误。

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.使用“1”参数调用“打开”时出现异常:“Microsoft Office Excel 无法访问文件‘C:\reports\1.xlsx’。可能的原因有多种:文件名或路径不存在。文件另一个程序正在使用。您要保存的工作簿与当前打开的工作簿同名。

I run the task with the same credentials I use to run the script in the Powershell command prompt.我使用与在 Powershell 命令提示符下运行脚本相同的凭据运行任务。 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.当我在任务计划程序中运行它时,它无法访问电子表格。

The file in question is readable/writeable for all users.有问题的文件对所有用户都是可读/可写的。 I've verified I can open the file in Excel with the same credentials.我已经验证我可以使用相同的凭据打开 Excel 中的文件。 If I make a new spreadsheet and put its name in as the $filename, I get the same results.如果我制作一个新的电子表格并将其名称作为 $filename,我会得到相同的结果。 I've verified that there are no instances of Excel.exe in Task Manager.我已验证任务管理器中没有 Excel.exe 的实例。

Oddly, if I use get-content, I don't have any problems.奇怪的是,如果我使用 get-content,我没有任何问题。 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

That works fine.那很好用。 So Get-ChildItem can open the file with no issue.因此 Get-ChildItem 可以毫无问题地打开文件。 ComObject can open the file if I run it manually, but not if it's run as task.如果我手动运行 ComObject 可以打开该文件,但如果它作为任务运行则不能。

I'm at a loss.我不知所措。 Any ideas?有任何想法吗?

I think you've hit a bug in Excel:我认为您在 Excel 中遇到了错误:

You have to create a folder (or two on a 64bit-windows):您必须创建一个文件夹(或在 64 位 Windows 上创建两个):

(32Bit, always) (32 位,始终)

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

(64Bit) (64位)

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.我遇到了同样的问题,这是我找到的唯一解决方案。

From TechNet Forums (via PowerShell and Excel Issue when Automating )来自TechNet 论坛自动化时通过PowerShell 和 Excel 问题

上述解决方案在我的 SCSM 2012 Scorch 环境中不起作用,而是我使用了 PSExcel ( https://github.com/RamblingCookieMonster/PSExcel ),它不依赖于安装 Excel 或 ComObject。

To extend what @TessellatingHeckler provided, you can run the following commands in Powershell(As Admin/Elevated) to create the folders before opening excel, in my script this fixed the issue:要扩展@TessellatingHeckler 提供的内容,您可以在 Powershell(以管理员身份/高级身份)中运行以下命令以在打开 excel 之前创建文件夹,在我的脚本中这解决了该问题:

New-Item -ItemType Directory -Force -Path C:\Windows\System32\config\systemprofile\Desktop
if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem)
{
    New-Item -ItemType Directory -Force -Path C:\Windows\SysWOW64\config\systemprofile\Desktop
}

I had to set my Scheduled Task to run 'only when user is logged on' (logged on to server as the service account that runs task then disconnect session) as it seems to be a limitation with the Task Scheduler and Excel.我必须将我的计划任务设置为“仅在用户登录时运行”(作为运行任务的服务帐户登录到服务器然后断开会话),因为它似乎是任务计划程序和 Excel 的限制。 It's a pretty lame workaround but it works.这是一个非常蹩脚的解决方法,但它有效。

To reiterate what TessellatingHeckler said.重申 TessellatingHeckler 所说的话。 I had to resolve this issue on a 64 bit system, so I used the following command which made the PowerShell script finally work via Task Scheduler:我必须在 64 位系统上解决这个问题,所以我使用以下命令使 PowerShell 脚本最终通过任务计划程序运行:

New-Item -ItemType Directory -Force -Path C:\Windows\SysWOW64\config\systemprofile\Desktop新项目 -ItemType Directory -Force -Path C:\Windows\SysWOW64\config\systemprofile\Desktop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM