%3CLINGO-SUB%20id%3D%22lingo-sub-509971%22%20slang%3D%22en-US%22%3EExecuting%20PowerShell%20command%20in%20web%20method%20of%20a%20web%20service%20hosted%20on%20SharePoint%202013%20farm%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509971%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Mar%2010%2C%202015%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EThis%20post%20is%20a%20contribution%20from%20Pavan%20Kumar%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%3C%2FP%3E%0A%20%20%3CP%3EI%20worked%20on%20a%20very%20interesting%20case%20where%20I%20had%20to%20execute%20powershell%20commands%20in%20web%20method%20of%20a%20web%20service.%20But%20while%20executing%20powershell%20command%20I%20get%20following%20error%3C%2FP%3E%0A%20%20%3CP%3E%22The%20type%20initializer%20for%20'System.Management.Automation.SessionStateScope'%20threw%20an%20exception.%22%3C%2FP%3E%0A%20%20%3CP%3EWhen%20I%20try%20the%20same%20on%20Sharepoint%202010%20it%20works%20fine%20but%20this%20fails%20only%20on%20Sharepoint%202013%20environments%3C%2FP%3E%0A%20%20%3CP%3EAfter%20a%20lot%20of%20digging%20I%20could%20find%20the%20resolution%3C%2FP%3E%0A%20%20%3CP%3EIn%20the%20web.config%20of%20my%20web%20application%20I%20changed%20the%20legacyCasModel%20to%20%22false%22%20in%20the%20below%20tag%3C%2FP%3E%0A%20%20%3CP%3E%3CI%3E%20%3CTRUST%20level%3D%22%26quot%3BFull%26quot%3B%22%20originurl%3D%22%26quot%3B%26quot%3B%22%20legacycasmodel%3D%22%26quot%3Btrue%26quot%3B%22%3E%3C%2FTRUST%3E%20%3C%2FI%3E%3C%2FP%3E%0A%20%20%3CP%3ETo%20know%20more%20about%20%E2%80%9ClegacyCasModel%E2%80%9D%20refer%20to%20this%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.web.configuration.trustsection.legacycasmodel(v%3Dvs.110).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20article%3C%2FA%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509971%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20TECHNET%20on%20Mar%2010%2C%202015%20This%20post%20is%20a%20contribution%20from%20Pavan%20Kumar%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%20I%20worked%20on%20a%20very%20interesting%20case%20where%20I%20had%20to%20execute%20powershell%20commands%20in%20web%20method%20of%20a%20web%20service.%3C%2FLINGO-TEASER%3E
Microsoft
First published on TECHNET on Mar 10, 2015

This post is a contribution from Pavan Kumar, an engineer with the SharePoint Developer Support team

I worked on a very interesting case where I had to execute powershell commands in web method of a web service. But while executing powershell command I get following error

"The type initializer for 'System.Management.Automation.SessionStateScope' threw an exception."

When I try the same on Sharepoint 2010 it works fine but this fails only on Sharepoint 2013 environments

After a lot of digging I could find the resolution

In the web.config of my web application I changed the legacyCasModel to "false" in the below tag

<trust level="Full" originUrl="" legacyCasModel="true" />

To know more about “legacyCasModel” refer to this article