Post Build Event Execute Powershell

Post build event execute PowerShell

Here is an example :

First of all : you must be aware of the fact that PowerShell must be configure to execute scripts. The following line allow PowerShell to execute scripts :

Set-ExecutionPolicy RemoteSigned

Special mention here : if you are running a 64bits system you've got to take care of the fact that 'devenv.exe' the Visual Studio 2010 executable is a 32Bits exe, so you need to allow PowerShell 32 to execute scripts.

Once here you can go in your project properties and configure post build as shown here under (sorry in french) :

Post build in VS 2010

For example :

Example of postbuild with powershell

Here is the file 'psbuild.ps1', it creates a 'test.txt' in the target path with the configuration name inside. I put in comment different ways to debug your postbuild script (message box, sound, message on the output)

param ([string]$config, [string]$target)

#[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#[void][System.Windows.Forms.MessageBox]::Show("It works.")
#[Console]::Beep(600, 800)
#Write-Host 'coucou'
set-content $target -Value $config -Force

PowerShell .ps1 file on Visual Studio post build event

You can reproduce the error in Powershell as follows:

"this is a string" -file "my.ps1"

It is taking the first as a string, the -file as the -f format flag and saying it doesn't have a value expression on the right for the format substitution.

Try like this:

& "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"

(as Keith notes, this will not work as this is run from a bat file than Powershell.)

Or just:

powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"

Call powershell script in post-built with parameters

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:

using System;
using System.Diagnostics;

class App
{
static int Main(string[] args)
{
Process process = Process.Start("PowerShell.exe", String.Join(" ", args));
process.WaitForExit();
return process.ExitCode;
}
}

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

csc .\PowerShell64.cs /platform:x64

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.:

c:\path\PowerShell64.exe -File "$(MSBuildProjectDirectory)\CreateSite.ps1" auto

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

Jenkins: How to execute powershell script after successful build

Actually, there's also a third... the one I've used and the best suited for your situation is probably the PowerShell Plugin



Related Topics



Leave a reply



Submit