【问题标题】:Scheduled task not run using x86 version of Powershell未使用 x86 版本的 Powershell 运行计划任务
【发布时间】:2016-02-04 16:10:35
【问题描述】:

我编写了一个 Powershell 脚本,它保存在 .ps1 文件中。它仅适用于位于

中的 32 位版本的 Powershell

%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe

脚本在我手动运行时有效,但它不是通过 Windows 任务计划程序 运行的。我以运行脚本的同一用户身份运行任务。在我的任务的 Action 部分中,我将上述地址作为 Program/script 并将我的 .ps1 文件的完整路径写为 Add Arguments(可选) 。但这似乎不起作用。我还尝试将我的 .ps1 文件的父文件夹作为 Start in 值但无济于事。

如何让任务计划程序使用 32 位版本运行我的 Powershell 脚本?

更新:我必须在这里补充一点,我的脚本实际上打开了一个 Excel 文件,刷新它然后关闭它。我知道在非交互式环境中使用 Excel 是个坏主意。但我仍然不知道这是否是我的脚本没有运行的原因。

【问题讨论】:

    标签: excel powershell scheduled-tasks


    【解决方案1】:

    高度怀疑 Excel 是这似乎不起作用的原因。让你的脚本做一些非 Excel 的事情(例如创建文件)并检查这部分是否执行得很好

    我在自动化 Excel 时遇到的两个主要问题:

    1. Create empty folders if they don't exist (excel automation bug)

    2. Ensure DCOM security settings are configured to allow Excel to run. 如果您以手动运行脚本的同一用户身份运行任务,则仍然需要此设置。

    如果未正确设置 DCOM 权限并将脚本作为自动任务运行,您将收到以下错误。 Saw this as session was transcribed, and transcription output to text file.

    New-Object : 检索具有 CLSID 的组件的 COM 类工厂 {00024500-0000-0000-C000-000000000046} 由于以下原因而失败 错误:80070005 访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))。

    【讨论】:

      【解决方案2】:

      您可以采用您的脚本来确定当前的 powershell 版本,并在必要时使用 32 位版本调用相同的脚本。将这些行放在脚本的顶部:

      # ensure the script is running with the 32bit powershell
      if ($env:Processor_Architecture -ne "x86")
      {
          $psx86 = Join-Path $env:SystemRoot '\syswow64\WindowsPowerShell\v1.0\powershell.exe'
          & $psx86 -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
          exit
      }
      

      注意:如果您的脚本需要它们,您可以将参数附加到 powershell 调用。

      【讨论】:

      • 但恐怕不是问题所在。问题是这整件事不喜欢被安排。
      • 您可以在任务调度程序中使用powershell -file ... 定期调用您的脚本。
      猜你喜欢
      • 1970-01-01
      • 2017-11-08
      • 2019-05-02
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2013-08-18
      相关资源
      最近更新 更多