Jul 2, 2013 at 10:58 PM
Edited Jul 2, 2013 at 10:59 PM
I am wondering if you would be so kind as to offer some advice since you are obviously awesome with SP custom timer jobs. I'm trying to setup a timer job for SP2013 and getting errors. When debugging the stack trace yields an error that is new to me with .NET 4.5 and reads as though I'm not coding the solution correctly more than I need to modify any config files to elevate code execution. Here is the error I'm receiving. Can you offer any guidance or suggestions?

Exception in Ent.Timer.TemplateThe type initializer for 'System.Management.Automation.SessionStateScope' threw an exception. System.InvalidOperationException: Dynamic operations can only be performed in homogenous AppDomain.
at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite'1 site, Object[] args)

at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

at System.Management.Automation.Language.PSVariableAssignmentBinder.ObjectRule(CallSite site, Object obj)

at System.Management.Automation.PSVariable..ctor(String name, Object value, ScopedItemOptions options, Collection'1 attributes)

at System.Management.Automation.SessionStateScope..cctor()

Nov 7, 2013 at 8:36 PM
I've encountered the same issue with trying to execute PowerShell code via System.Management.Automation within the context of a timer job in SP2013. The workaround (hack) for this to work is to remove the <NetFX40_LegacySecurityElement> node from the OWSTimer.exe.config file and restart the SPTimer service. I've reached out to a SharePoint PFE and he informed me that doing this is supported, but not generally recommended. So far I haven't encountered any issues, but my code works.

In case you're wondering, the path to OWSTimer.exe.config should be C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN\OWSTimer.EXE.Config.