5

I want to use a powershell script in a build event in Visual Studio, and I need to use some macro defined in Visual Studio itself.

This is my powershell script sample:

"Updating version for Project:"
"$ProjectDir"

Then in pre-build event of the project I call it in this way:

powershell.exe -file "$(SolutionDir)Resources\UpdateVersion.ps1" -path $(SolutionDir)

When I build the project, I call correctly the script but the string containing the project path is empty.

Is there a way to pass all visual studio macros to the powershell script without passing them explicitly as script arguments (I must use a lot of them and it will be tedious)?

Jepessen
  • 11,744
  • 14
  • 82
  • 149
  • Just to clarify, "the string containing the project path is empty," is referring to what I assume should be `$SolutionDir`, correct? – gravity Jul 06 '16 at 13:53
  • Yes, if I try to print a visual studio macro I can see that's empty. – Jepessen Jul 06 '16 at 13:54
  • So, then we would need to see and understand the steps prior to attempting to run `UpdateVersion,` as `$SolutionDir` needs to be populated *prior* to calling the version update. Where are you assigning `$SolutionDir` a value? – gravity Jul 06 '16 at 13:56
  • It's a macro defined inside Visual Studio, and I call the powershell script from inside Visual Studio build event. I don't define it inside the script. At the moment I pass it through an argument, but considering that I use a lot of these macros I'd like to know if there's a way to pass them directly instead that using script parameters. – Jepessen Jul 06 '16 at 14:05

1 Answers1

0

A late answer but I hope it helps someone.

In your Pre-Build event you're passing $(SolutionDir) using the "-path" argument but in your PowerShell script, you never use the "-path" argument. It looks like you tried to reference it as "$ProjectDir".

Explaining how to use named arguments in PowerShell is outside the scope of this question so I will switch to using positional arguments.

Change your PowerShell script to be:

param($solution, $project)
Write-Host $solution
Write-Host $project

Then, change your build event to be:

powershell.exe -file "$(SolutionDir)Resources\UpdateVersion.ps1" $(SolutionDir) $(ProjectDir)
John Vottero
  • 845
  • 1
  • 7
  • 24