I have a SP2013 timer job where I'm executing some SharePoint PowerShell commands. The same code that used to work in SharePoint 2010 will not work in SharePoint 2013 (Server 2012) - when the job is executed, the following exception occurs:
The type initializer for 'System.Management.Automation.SessionStateScope' threw an exception.
Stack trace:
at System.Management.Automation.SessionStateScope.AddSessionStateScopeDefaultVariables()
at System.Management.Automation.SessionStateScope.GetPrivateVariables()
at System.Management.Automation.SessionStateScope.SetVariable(String name, Object value, Boolean asValue, Boolean force, SessionStateInternal sessionState, CommandOrigin origin, Boolean fastPath)
at System.Management.Automation.SessionStateInternal.InitializeSessionStateInternalSpecialVariables(Boolean clearVariablesTable)
at System.Management.Automation.SessionStateInternal..ctor(SessionStateInternal parent, Boolean linkToGlobal, ExecutionContext context)
at System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine, PSHost hostInterface)
at System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss)
at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper()
at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall)
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection
1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection
1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
...
The problematic part seems to be that the project references System.Management.Automation
and uses PowerShell.
From googling, this seems to be a SP2013-specific bug, likely an OWSTIMER bug, as indicated by this post that also offers a hack-around (unfortunately not acceptable to me):
http://sppowershelltimerjob.codeplex.com/discussions/448895
- Is this recognized as an internal SharePoint 2013 bug?
- Can it be fixed?