简体   繁体   English

如何使用PowerShell读取MSBuild PropertyGroup的值?

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

I have the following MSBuild ( TestBuild.xml ) file: 我有以下MSBuild( TestBuild.xml )文件:

<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: 我希望能够得到的值VersionFile在这种情况下,节点Version.cs我一直使用XPath打希望下面将工作:

$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. Xml具有名称空间,因此您必须限定元素名称。 Use Select-Xml to evaluate an XPath with a namespace: 使用Select-Xml评估具有名称空间的XPath:

$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. 我认为您需要执行MSBuild,运行自定义目标并捕获结果值。

Treating the msbuild project as an xml file may not get you the desired result. 将msbuild项目视为xml文件可能无法获得预期的结果。 Consider the situation where the property is defined dynamically, or is calculated conditionally, bases on configuration settings (debug vs release, etc..). 考虑基于配置设置(调试与发布等)动态定义或有条件计算属性的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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