Excel 宏 运行 通过 powershell 但当 运行 通过 windows 任务调度程序
Excel macros run through powershell but not when run by windows task scheduler
我有一个脚本可以检查文件夹中是否有 excel 个文件,如果这个 "threshold" 大于 0,那么来自另一个 excel 文件的宏是 运行与这些 excel 文件夹交互。
当我 运行 通过 powershell ISE 手动处理时,它工作正常但是当我使用 windows 任务调度程序时,powershell 脚本 运行s 但 excel 宏调用没有 运行。有什么建议为什么会这样吗?此进程用于 运行 在 windows 2008 服务器上正常,但已迁移到 windows 服务器 2012,并且不会 运行 正确
if ($count -gt $threshold){
$excel = new-object -comobject excel.application
$workbook = $excel.workbooks.open("D:\TimesheetService\IS-FS - AutoTimesheetLoader v2.3 - UAT.xlsm")
$worksheet = $workbook.worksheets.item(1)
$excel.Run("ImportTime")
$workbook.close($false)
$excel.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
}
您不能从计划任务中使用 COM 自动化 Excel 库 (new-object -comobject excel.application
),除非该任务是 运行 在当前登录用户的window站(会话).
也就是说,为了从计划任务中使用 COM Excel 库,您必须为其选择 Run only when user is logged on
选项,您可能不需要,原因有二:
它将您的任务限制为 运行 只有当有人恰好登录时。
当前登录的用户将看到任务的window,因为它运行s - 除非你采取额外的步骤来隐藏它(这不能用 powershell.exe
本身来完成)。
注意:有一种解决方法目前有效,但不受支持 ,这就是为什么 最好避免 it - 请参阅 this answer 相关的 superuser.com 问题。
因此,考虑没有此限制的替代方案,例如DocumentFormat.OpenXml Nuget package。
有关背景信息,请参阅 this Microsoft support article。
我也在尝试做同样的事情。这对我有用 https://www.jonashendrickx.com/2016/04/07/when-run-as-scheduled-task-excel-wont-save-with-powershell/
最后两步正是我所需要的。检查以确保这些文件夹存在
在 32 位和 64 位操作系统上:
C:\Windows\System32\config\systemprofile\Desktop
在 64 位操作系统上:
C:\Windows\SysWOW64\config\systemprofile\Desktop
我有一个脚本可以检查文件夹中是否有 excel 个文件,如果这个 "threshold" 大于 0,那么来自另一个 excel 文件的宏是 运行与这些 excel 文件夹交互。
当我 运行 通过 powershell ISE 手动处理时,它工作正常但是当我使用 windows 任务调度程序时,powershell 脚本 运行s 但 excel 宏调用没有 运行。有什么建议为什么会这样吗?此进程用于 运行 在 windows 2008 服务器上正常,但已迁移到 windows 服务器 2012,并且不会 运行 正确
if ($count -gt $threshold){
$excel = new-object -comobject excel.application
$workbook = $excel.workbooks.open("D:\TimesheetService\IS-FS - AutoTimesheetLoader v2.3 - UAT.xlsm")
$worksheet = $workbook.worksheets.item(1)
$excel.Run("ImportTime")
$workbook.close($false)
$excel.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
}
您不能从计划任务中使用 COM 自动化 Excel 库 (new-object -comobject excel.application
),除非该任务是 运行 在当前登录用户的window站(会话).
也就是说,为了从计划任务中使用 COM Excel 库,您必须为其选择 Run only when user is logged on
选项,您可能不需要,原因有二:
它将您的任务限制为 运行 只有当有人恰好登录时。
当前登录的用户将看到任务的window,因为它运行s - 除非你采取额外的步骤来隐藏它(这不能用
powershell.exe
本身来完成)。
注意:有一种解决方法目前有效,但不受支持 ,这就是为什么 最好避免 it - 请参阅 this answer 相关的 superuser.com 问题。
因此,考虑没有此限制的替代方案,例如DocumentFormat.OpenXml Nuget package。
有关背景信息,请参阅 this Microsoft support article。
我也在尝试做同样的事情。这对我有用 https://www.jonashendrickx.com/2016/04/07/when-run-as-scheduled-task-excel-wont-save-with-powershell/
最后两步正是我所需要的。检查以确保这些文件夹存在
在 32 位和 64 位操作系统上:
C:\Windows\System32\config\systemprofile\Desktop
在 64 位操作系统上:
C:\Windows\SysWOW64\config\systemprofile\Desktop