Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell .ps1 file on Visual Studio post build event

I am trying to execute the following post build event code but I am getting an non-useful error :

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

I have run the following PS script before I try :

Set-ExecutionPolicy unrestricted

What am I missing?

UPDATE

This is strange, I am not getting an error on VS now. but the script is not working. When I run it with powershell console I get the following error :

enter image description here

like image 463
tugberk Avatar asked Sep 02 '11 15:09

tugberk


2 Answers

Visual Studio writes the post-build event script to a .BAT file and executes that using cmd.exe. So using & "<path-to-powershell>" won't work. Just execute:

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

And if you think you're likely to run into execution policy issues on other machines that build the solution, consider going this route:

Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1" 
like image 93
Keith Hill Avatar answered Sep 24 '22 14:09

Keith Hill


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"
like image 25
manojlds Avatar answered Sep 25 '22 14:09

manojlds