Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post build event execute PowerShell

Is it possible to set up a .NET project with a post build event to execute a powershell script? I am using this script to generate some files.

Also can I pass whether it's a debug or release build to script. An example of this would be great.

like image 748
amateur Avatar asked Jun 28 '11 00:06

amateur


2 Answers

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 
like image 127
JPBlanc Avatar answered Sep 24 '22 23:09

JPBlanc


command Set-ExecutePolicy will temporarily set execution policy under current session. If you set this in powershell and run post build command in vs you will still get not allowed. So set first then run your ps1 script like bellow

powershell -ExecutionPolicy Unrestricted $(ProjectDir)Deploy.ps1 -ProjectDir $(ProjectDir) -TargetPath $(TargetPath) 
like image 20
Ariwibawa Avatar answered Sep 24 '22 23:09

Ariwibawa