Powershell Script as a scheduled task errors when I try to connect to Excel worksheet to add data.

Mike McMillan 0 Reputation points
2024-03-20T20:49:38.69+00:00

My PowerShell script runs fine when I execute it manually. But when I run it from the task scheduler either manually or triggered it errors.

$excel = New-Object -ComObject excel.application

$workbook = $excel.Workbooks.Add()

Errors start here: All three of the following lines generate an error.

$UserInfoSheet = $workbook.Worksheets.Item(1)

$UserInfoSheet.Name = 'Accounts'

$UserInfoSheet.Activate() | Out-Null

Event log:

Event log “Faulting application name: EXCEL.EXE, version: 16.0.14332.20651, time stamp: 0x65df7edc

Faulting module name: ntdll.dll, version: 10.0.17763.5458, time stamp: 0xfb111856

Exception code: 0xc0000005

Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,162 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.