title: "Unable to get the Open property of the Workbooks class" when automated
body: <p>I have some PowerShell scripts that invoke SQL commands, take the results and put them into a CSV file, then the CSV file is put into an excel workbook and it's emailed out to a distribution list. I had no issues running these reports through Windows Scheduled tasks on my older Windows 2008 server running SQL 2008. But I have migrated over to Windows 2016 running SQL 2016. Now when I run this process through Scheduled tasks I get the following error:</p>
  
  <pre><code>Unable to get the Open property of the Workbooks class
  At C:\PowerShell\scrpits\ArtiReport3.ps1:607 char:1
  + $workbook = $excel.Workbooks.Open($csvFilePath)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : OperationStopped: (:) [], COMException
  + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  </code></pre>
  
  <p>If I run the PowerShell script manually I have no issues and everything runs fine. I'm using the same login to run the scripts manually as I do through scheduled tasks. Here is the script.</p>
  
  <pre><code>$query = "*SQL Query runs here*"
  
  #Edit these peramters for the server this will be running on#
  $smtpServer = "*server*";
  $smtpFrom = "I3Reports@server.com";
  $smtpTo = "*email list here*”
  $messageSubject = "I3 Report";
  
  #create anonymus loging for sending e-mail#
  $User = "anonymous";
  $PWord = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
  $Creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pword
  
  
  $date = (Get-Date).AddDays(-1).ToString('yyyy-MM-dd')
  $date = $date+"_I3Report.xls";
  
  $csvFilePath = "c:\Scripts\queryresults.csv"
  $excelFilePath = "c:\scripts\$date"
  
  
  $instanceName = "*server*"
  Import-Module "sqlps"
  $results = Invoke-Sqlcmd -QueryTimeout 7200 -Query $query -ServerInstance $instanceName
  
  # Output to CSV
  $results | export-csv  $csvFilePath -Delimiter "    " -NoTypeInformation
  #this line will remove all the quotation marks from the csv file
  (Get-Content $csvFilePath) | % {$_ -replace '"', ""} | out-file -FilePath $csvFilePath -Force 
  
  # Convert CSV file to Excel
  
  
  $excel = New-Object -ComObject excel.application 
  $excel.visible = $False 
  $excel.displayalerts=$False 
  $workbook = $excel.Workbooks.Open($csvFilePath) #&lt;-- Program fails here
  $workSheet = $workbook.worksheets.Item(1) 
  #$workSheet.cells.item(3,3) = "HOPLA"
  
  #for freezing pane#
  $workSheet.application.activewindow.splitcolumn = 0
  $workSheet.application.activewindow.splitrow = 1
  $workSheet.Range("A2").application.activewindow.freezepanes = $true
  $resize = $workSheet.UsedRange 
  $resize.EntireColumn.AutoFit() | Out-Null 
  $xlExcel8 = 43
  $workbook.SaveAs($excelFilePath, $xlExcel8)
  $workbook.Close()
  $excel.quit() 
  $excel = $null
  
  send-mailmessage -from $smtpFrom -to $smtpTo -subject "$messageSubject" -body "Attachment" -Attachments $excelFilePath -smtpServer $smtpServer -Credential $creds;
  </code></pre>
  
  <p>As mentioned this works when I run it in PowerShell manually, but through scheduled tasks it gets that error and it references the section I noted in the code. I've been working on this for days and I can't seem to figure out what is causing the issue. Any help or suggestions is welcomed. Thanks for taking the time.   </p>