Context
You are developing a SharePoint 2010 application using Visual Studio 2010 on a Windows 2008 64-bit server. You want to run a post-deployment script (PowerShell for example) for debugging:
The Problem
When you add a script, for example:
powershell $(ProjectDir)\PowerShellScript\MySuperPowerShellScript.ps1
You always get an error when running the script:
Error occurred in deployment step ‘Run Post-Deployment Command’: The command "powershell $(ProjectDir)\PowerShellScript\MySuperPowerShellScript.ps1" exited with error code: 1.
This error is due to the fact that VS2010 runs in 32-bit mode, as well as its post-deployment scripts, and the SharePoint 2010 APIs run in 64-bit mode. This creates errors when executing the script.
The Solution
The script must be run in 64-bit mode to avoid any errors. To do this, we will use MSBuild.
We will create the script MySuperPowerShellScript.msbuild:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Install" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Install">
<Exec Command="powershell .\MySuperPowerShellScript.ps1" />
</Target>
</Project>
This script, when called, will run the PowerShell script in 64-bit mode and no errors will be generated!
Here is the command line to run it from VS2010:
`%WinDir%\Microsoft.NET\Framework64\v4.0.30319\MSBuild
$(ProjectDir)\PowerShellScript\MySuperPowerShellScript.msbuild`
And that’s it!
Happy post-deployment!