Powershell Script as a scheduled task errors when I try to connect to Excel worksheet to add data.
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