简体   繁体   中英

How can I read the value of an MSBuild PropertyGroup using PowerShell?

I have the following MSBuild ( TestBuild.xml ) file:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  ...
  <Import Project="folderA\A.targets"/>

  <!-- bunch of stuff -->
  <PropertyGroup>    
    <ApplicationName>MyApp</ApplicationName>
    <SolutionName>MySolution</SolutionName>
    <VersionFile>Version.cs</VersionFile>
    <TargetPackageVersion>$(RELEASE_VERSION)</TargetPackageVersion>
    <BuildPlatform Condition=" '$(BuildPlatform)' == '' ">x86</BuildPlatform>
    <TestAssembliesPattern>*Tests*.dll</TestAssembliesPattern>
    ...    
  </PropertyGroup>

  <!-- bunch of more stuff -->
  <PropertyGroup>
    <ConfigurationContentFolder>$(MSBuildProjectDirectory)\Configuration</ConfigurationContentFolder>
    <PluginName>ABC</PluginName>
  </PropertyGroup>
  ...
</Project>

I want to be able to get the value of the VersionFile node in this case Version.cs I have been playing with XPath hoping that the following would work:

$xdoc = [xml] $xdoc = get-content ".\TestBuild.xml"
$xdoc.SelectSingleNode("//VersionFile") | select { $_.InnerText }
$xdoc.SelectSingleNode("VersionFile") | select { $_.InnerText }

But so far I have not had any luck. Any help is much appreicated.

The Xml has a namespace so you've got to qualify the element name. Use Select-Xml to evaluate an XPath with a namespace:

$xml = [xml](get-content ".\TestBuild.xml")
$ns = @{ msb = "http://schemas.microsoft.com/developer/msbuild/2003" }
Select-Xml $xml -Namespace $ns -XPath "//msb:VersionFile" | foreach { $_.Node.InnerText }

I would think you need to execute MSBuild, run a custom target, and capture the resulting value.

Treating the msbuild project as an xml file may not get you the desired result. Consider the situation where the property is defined dynamically, or is calculated conditionally, bases on configuration settings (debug vs release, etc..).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM