Search code examples
powershellcomtaskscheduler

Cannot access COM interface from Task Scheduler


This line cannot complete when a powershell script is called in a scheduled task, but it works when the script is run on its own:

$App = New-Object -comObject Excel.Application

The scheduled task is stuck "Running," and this behavior appears with apps other than Excel. When I write log outputs before and after this line, it shows that the line never finishes.

I have been able to run other powershell scripts from Task Scheduler, but I'm not familiar enough with this or COM to know what might be going wrong.

What is different in the context of Task Scheduler that would cause problems with the COM interface? How would I troubleshoot this?

Update: If I let the task sit for ~5 minutes my log file shows it eventually gets past the line. It can make it past following COM commands, but they don't actually work. The definition of the COM object would give an error: "80080005 Server execution failed."


Solution

  • SOLVED: It was a DCOM issue. This was fixed by opening Component Services and going to Component Services > Computers > My Computer > DCOM Config > (right click the relevant application) > Properties. Then under the Identify tab, I set the radio button to interactive user.

    post relevant to solution: https://superuser.com/questions/579900/why-cant-excel-open-a-file-when-run-from-task-scheduler