1
votes

I have written a Powershell script and it is saved in a .ps1 file. It only works with the 32-bit version of the Powershell that is located in

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

The script works when I run it manually but it is not run via 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). 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.

How can I tell Task Scheduler to run my Powershell script using the 32-bit version?

UPDATE: I have to add here that my script actually opens an Excel file, refreshes it and then closes it. I know that using Excel in a non-interactive environment is a bad idea. But I still don't know if this is the reason my script is not run.

2

2 Answers

2
votes

Highly suspect Excel is the reason this appears not to work. Have your script do something non-Excel (e.g. create file) and check if this part was executed fine

Two major gotchas I've come across when automating 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. 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. 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)).

1
votes

You could adopt your script to determine the current powershell version and invoke the same script with the 32bit version if necessary. 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.