Call powershell script in post-built with parameters

Question:

I’m trying to get Powershell to run my PS script in post built – but somehow it doesn’t work like it’s supposed to:

Following command in Post-Build:

(inserted line break for better reading)

The command executes the powershell script sucessfully, but what it can’t do is run the commands within (Output from Build):
Rund Post-Build Command:

And following that are many errors because all subsequent commands need the Sharepoint Snap-In.

  • When running powershell C:\path\CreateSite.ps1 auto from cmd – everything works.
  • When opening powershell.exe and running C:\path\CreateSite.ps1 auto – everything works.
  • When right clicking CreateSite.ps1 –> run with powershell – everything works.

The relevant line in the script is simply Add-PsSnapin Microsoft.SharePoint.PowerShell.

How can I just run the darn script (and get it to include the PSSnapIn) passing it a parameter in Visual Studio post-build?

Answer:

Because of file system virtualization, you can’t really specify the path to the 64-bit version of PowerShell from a 32-bit process (ie Visual Studio – which hosts the msbuild engine). One hack-ish way to work around this is to create a 64-bit launcher that runs as 64-bit and will launch the 64-bit version of PowerShell. Here’s a simple C# program that will do this:

Be sure to compile this as 64-bit like so:

Then, from your post-build event execute this launcher exe passing it the parameters you want to invoke 64-bit PowerShell with. Also, with PowerShell 2.0 I would recommend using the File parameter to execute a script e.g.:

That said, surely there has to be some other way (utility) that launches exes from a 64-bit process.

Source:

Call powershell script in post-built with parameters by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply