none
Powershell Scheduled Task to convert xls to csv fails with ExceptionHandlingOps.CheckActionPreference RRS feed

  • Question

  • Hi,

    I have a scheduled task to run a powershell script and convert xls file to csv.

    If I run the task as "Run whether user is logged on or not", it fails 

    Can someone help me to resolve this issue.

    I am using service account hence cannot schedule a task with "Run only when user is logged in"

    Script :

    $excelApp = New-Object -ComObject Excel.Application
    $excelApp.DisplayAlerts = $false
    $excelApp.Visible = $true
    $workbook = $excelApp.Workbooks.Open("$ExportUsers")
    $sheet = $workbook.sheets.item(1)
    $sheet.Activate()
    $csvFilePath = $ExportUsers -replace "\.xls$", ".csv"

    try {
     $workbook.SaveAs("$csvFilePath", [Microsoft.Office.Interop.Excel.XlFileFormat]::xlCSV)
     $excelApp.ActiveWorkBook.Saved = $true
     $excelApp.WorkBooks.Close()
     $excelApp.Quit()

    }
    catch {
     throw $_.Exception
    }

    Error :

    Error Occured: System.Management.Automation.RuntimeException: You cannot call a method on a null-valued expression.
       at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
       at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:41 PM This is not "troubleshoot a script I found on the Internet for me" forum
    Sunday, October 1, 2017 1:05 PM

All replies