Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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:

  1. 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.
  2. 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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

So simple that I missed it.

The correct syntax of the version argument is :

powershell.exe -version 2.0

Everything goes fine now...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.