简体   繁体   English

计划的任务无法使用x86版本的Powershell运行

[英]Scheduled task not run using x86 version of Powershell

I have written a Powershell script and it is saved in a .ps1 file. 我已经编写了Powershell脚本,并将其保存在.ps1文件中。 It only works with the 32-bit version of the Powershell that is located in 它仅适用于位于以下位置的Powershell的32位版本

%SystemRoot%\\syswow64\\WindowsPowerShell\\v1.0\\powershell.exe 的%SystemRoot%\\ SysWow64资料\\ WindowsPowerShell \\ V1.0 \\ powershell.exe

The script works when I run it manually but it is not run via Windows Task Scheduler . 当我手动运行脚本时,该脚本可以运行,但不能通过Windows Task Scheduler运行。 I am running the task as the same user that runs the script. 我以与运行脚本相同的用户身份运行任务。 In the Action part of my task I put the above address as Program/script and I write the full path of my .ps1 file as Add Arguments (optional) . 在任务的“操作”部分,我将上面的地址作为Program / script放置,并将.ps1文件的完整路径写为Add Arguments(可选) But it does not seem to work. 但这似乎不起作用。 I have also tried with putting the parent folder of my .ps1 file as Start in value to no avail. 我还尝试将.ps1文件的父文件夹作为Start in value无效。

How can I tell Task Scheduler to run my Powershell script using the 32-bit version? 如何告诉Task Scheduler使用32位版本运行Powershell脚本?

UPDATE : I have to add here that my script actually opens an Excel file, refreshes it and then closes it. 更新 :我必须在这里添加我的脚本实际上打开一个Excel文件,刷新它,然后关闭它。 I know that using Excel in a non-interactive environment is a bad idea. 我知道在非交互式环境中使用Excel是一个坏主意。 But I still don't know if this is the reason my script is not run. 但是我仍然不知道这是否是我的脚本未运行的原因。

Highly suspect Excel is the reason this appears not to work. 高度怀疑Excel是此功能似乎无效的原因。 Have your script do something non-Excel (eg create file) and check if this part was executed fine 让脚本执行非Excel的操作(例如创建文件),并检查此部分是否执行良好

Two major gotchas I've come across when automating Excel: 自动化Excel时遇到的两个主要陷阱:

  1. Create empty folders if they don't exist (excel automation bug) 创建空文件夹(如果不存在)(Excel自动化错误)

  2. Ensure DCOM security settings are configured to allow Excel to run. 确保将DCOM安全设置配置为允许Excel运行。 This is still required if you are running task as same user who manually runs script. 如果您以与手动运行脚本的用户相同的身份运行任务,则仍然需要这样做。

When DCOM permissions are not set correctly and running the script as an automated task, you will get the below error. 如果未正确设置DCOM权限并将脚本作为自动任务运行,则会出现以下错误。 Saw this as session was transcribed, and transcription output to text file . 看到会话被转录后,转录结果输出到文本文件

New-Object : Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. 新对象:由于以下错误,检索具有CLSID {00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80070005访问被拒绝。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。

You could adopt your script to determine the current powershell version and invoke the same script with the 32bit version if necessary. 您可以采用脚本来确定当前的Powershell版本,并在必要时使用32位版本调用相同的脚本。 Put these lines on the top of your script: 将这些行放在脚本的顶部:

# 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
}

Note: You can append parameter to the powershell invoke in case your script requires them. 注意:您可以将参数附加到powershell调用中,以防脚本需要它们。

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

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