In a SharePoint 2010 project, using VS 2012, I set up a post build event:
"%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe" -version 2 -command "& {Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0; & '$(SolutionDir)somescript.ps1' -Web '$(SharePointSiteUrl)'}"
Basically, the script perform some clean up on the target sharepoint site.
This was working well with PowerShell V2 on Windows 2008 R2, but now that I'm working on Windows 2012 R2, and thus with PowerShell V3, the script does not works anymore.
I get this error:
The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.
There are two cases, from my understanding, that can cause this issue:
- The script runs in 32 bits mode (and VS is 32 bits). I solved this issue by using
%WINDIR%\SysNative
to target the 64 bits version event from the 32 bits process. - The .Net runtime is v4. I also solved this issue by forcing PowerShell V2 with the
-version 2
command line argument.
Is there any other case where this errors can appears?
How can I fix my post build event?