Excel.Application不使用SQL作业在PowerShell中工作,但可以从命令行工作

nos*_*hcd 3 sql-server powershell

我正在使用PowerShell解锁锁定的Excel电子表格.当我从作业运行脚本(使用我自己作为代理用户)时,脚本的Excel部分不会运行.但是,当我直接从命令行执行相同的脚本时,它可以正常工作.我的代码是:

#Unlock Attatchment 
$x = New-Object -comObject Excel.Application
$x.visible = $false
$x.DisplayAlerts = $False
$workbook = $x.Workbooks.Open($spreadsheetFileName,1,$false,
                              5,$spreadsheetPassword,$spreadsheetPassword)
$workbook.SaveAs($tmpFileName,1,$null,$null,$null,$null,$null,
                $x.XlSaveConflictResolution.xlLocalSessionChanges,
                $null,$null,$null,$null)
$x.Workbooks.Close()
$x.Quit()
Run Code Online (Sandbox Code Playgroud)

编辑:我添加了日志记录.以下是日志结果:

Exception calling "Open" with "6" argument(s): Microsoft Office Excel cannot access the file "_____"
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.
Run Code Online (Sandbox Code Playgroud)

nos*_*hcd 5

好!原来解决方案如下(如果这显示在搜索结果中):

  1. 添加目录: C:\Temp
  2. 添加目录: C:\Windows\SysWOW64\config\systemprofile\Desktop

这应该解决问题.