How to set a value for MSBuild property using Powe

2019-08-11 07:43发布

问题:

I have a property to specify the build drive:

<PropertyGroup>
    <BuildDrive Condition="'$(BuildDrive)'==''">Y:</Group>
</PropertyGroup>

If I want to change the build drive using a batch file, I can do as follows:

@echo off

set buildDrive=H:

:: Then call MSBuild

Msbuild /t:BuildTarget %Projectfile% %Logger%

Now I want achieve the same using PowerShell.

I tried as follows in my PowerShell script, build.ps1:

$BuildDrive=H:
MSbuild /t:BuildTarget $ProjectFile $Logger

But it is not honoring the drive letter provided through $BuildDrive. I knew I could achieve it if I passed a parameter as follows, but when the number of properties are more, this approach was not handy.

$BuildDrive=H:
Msbuild /t:BuildTarget /p:BuildDrive=$BuildDrive $projectfile $logger

How do I pass a PropertyGroup value through PowerShell?

回答1:

You are setting environment variables. These are available as properties in MSBuild.

You can do the following in PowerShell:

$env:BuildDrive="H:"