Question

I'm currently working on automating a powershell script to make a dump of a small DB. The database is accessible through a company Sharepoint and I am leveraging an IQY file exported from said Sharepoint to read the database contents. When I run the script manually (i.e. calling it from the command line), it works with no issues, but when I try and start the script from Task Scheduler, the following exception is raised:

System.Reflection.TargetInvocationException

the line that is generating the exception is:

$iqy = $xl.Workbooks.Open($query, 2, $true)

Where $xl is a new Excel.Application ComObject, created correctly and $query is a string containing the full file path of the iqy file.

As for my configuration, I'm trying to run my script on a Windows Server 2008R2, using a local administrator user (same user that is supposed to launch the scheduled script), which is also authorized to access the Sharepoint. I'm running Powershell 3.0. I'm not loading any Sharepoint-related snapins.

The only answers I found poking around hinted at granting "Trust" to the IQY file location and the Sharepoint itself in Excel. That did not help. The Exception also seems to point at a sharepoint issue, but since the script runs fine when started manually, I cannot understand how it could be anything to do with Sharepoint. Am I missing some intricacy of the Task Scheduler?

Any help would be greatly appreciated.

Was it helpful?

Solution

Never mind, I seem to have found an answer.

If anyone is encountering the same issue, it appear the Excel Com Object has a bug that does not allow it to run from the Task Scheduler if you set it to run regardless of whether the user is logged in. To circumvent that create the following 2 folders on the machine where the script is supposed to run:

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

After creating the folders, it worked as expected.

Source: http://social.technet.microsoft.com/Forums/windowsserver/en-US/aede572b-4c1f-4729-bc9d-899fed5fad02/run-powershell-script-as-scheduled-task-that-uses-excel-com-object?forum=winserverpowershell

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top