8

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

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • 2
    non-useful error? Neigh-on impossible to answer without *some* clue as what problem you're getting. – Jamiec Sep 02 '11 at 15:28
  • 1
    Not able to answer this [but I found a useful link for you](http://davidfrette.wordpress.com/2011/01/20/creating-powershell-pre-build-and-post-build-events-for-visual-studio-projects/) – Jamiec Sep 02 '11 at 15:43

3 Answers3

17

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" 
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • My bad, forgot about bat and also that OP had tried it in Powershell console. So should the file part be `$($SolutionDir)` ( I changed it from OP's initial one thinking it was PS ), but I suppose VS / MSBuild would need it in a different way? `$(SolutionDir)`? – manojlds Sep 03 '11 at 04:25
  • No biggie. Yeah, it should be `$(SolutionDir)`. That's how you reference a property in MSBuild. Fixed. Thanks. – Keith Hill Sep 04 '11 at 06:02
  • according to: http://technet.microsoft.com/en-us/library/hh847736.aspx "File must be the last parameter in the command" so it should be: Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1" BTW. thanks to this you can add params to ps1 – Andrzej Martyna Oct 25 '13 at 07:02
9

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"
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • so the problem is on "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"? – tugberk Sep 02 '11 at 15:41
  • @tugberk - It is being treated as a string. See my updated answer giving suggestions for you to try. – manojlds Sep 02 '11 at 15:42
  • I can get this to work until I have parameters with spaces. How would I call this with parameters with spaces? – Rhyous Mar 16 '19 at 21:17
1

Before calling power-shell script from visual studio, set the ExecutionPolicy to unrestricted from power-shell window like this...

Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: unrestricted;

the call power-shell script in the following manner...

powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)

enter image description here

then in the script, you can always read the parameter like this...

param([string]$SolutionDir,
     [string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");
Narottam Goyal
  • 3,534
  • 27
  • 26