1

I have a PowerShell script to convert a .xls file to .txt file. It runs fine in PowerShell and as a scheduled task when "run only when the user logged on" is checked as my user account, but if I try the system and run whether logged on or not, it doesn't work.

I've tried different arguments and setting some security flags on the account already.

Some debugging shows this when running as system user:

Microsoft Excel cannot access the file 'C:\test\test.xls'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook.

At C:\test\t.ps1:5 char:1 + $WorkBook = $Excel.Workbooks.Open($file.Fullname) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression. At C:\test\t.ps1:6 char:1 + $Workbook.SaveAs('c:\test\t.txt', 42) # xlUnicodeText + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

#excel processing
$Excel = New-Object -ComObject Excel.Application 
$Excel.visible = $false
$Excel.DisplayAlerts = $false
$file = Get-ChildItem 'c:\test\test.xls'
$WorkBook = $Excel.Workbooks.Open($file.Fullname)
$Workbook.SaveAs('c:\test\t.txt', 42)   # xlUnicodeText
# cleanup
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkBook) |     Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

#powershell -ExecutionPolicy ByPass -File C:\test\t.ps1

The expected result creates a new txt file in the test folder, the actual result just produces nothing.

Matthew
  • 1,412
  • 2
  • 20
  • 35
serulin
  • 11
  • 2
  • 1
    [This](https://stackoverflow.com/questions/15054332/how-to-run-excel-vba-code-from-a-windows-service) might give you a clue.. – Peter Schneider Oct 11 '19 at 07:54
  • 1
    Possible duplicate of [How to run excel vba code from a Windows Service](https://stackoverflow.com/questions/15054332/how-to-run-excel-vba-code-from-a-windows-service) – iRon Oct 11 '19 at 08:40

0 Answers0