计划任务不能使用x86版本的Powershell运行

问题描述:

我写了一个Powershell脚本,它保存在.ps1文件中。它仅适用于PowerShell的的32位版本位于计划任务不能使用x86版本的Powershell运行

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

,当我手动运行该脚本,但它不是通过Windows任务调度运行。我以运行该脚本的同一用户的身份运行该任务。在动作我的任务的一部分,我把上述地址作为程序/脚本和我写我的.ps1文件的完整路径为添加参数(可选)。但它似乎并不奏效。我也试着把我的.ps1文件的父文件夹作为开始值无济于事。

如何告诉Task Scheduler使用32位版本运行我的Powershell脚本?

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

您可以采用脚本来确定当前的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调用中。

+0

但是,恐怕不是问题所在。问题是这整个事情不喜欢被安排。 – Disasterkid

+0

您可以在任务计划程序中使用'powershell -file ...'调用脚本。 –

高度怀疑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 : Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).