如何在PowerShell中向csproj添加新的PropertyGroup

3

我有以下ps1文件。这个文件存在三个问题。

问题1:如何在XML中使用单引号和双引号?我搜索了谷歌并发现需要添加一个额外的单引号。我尝试过了但没有成功。

问题2:当我将新的PropertyGroup作为子节点附加到Project节点时,我得到了“错误类型”的错误。我该如何修复它。

问题3:我可以将多个PropertyGroup附加到Project节点吗?

$dir = "C:\Work\scripttest\output\"
$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

$configs = [xml]"<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Dev-1|AnyCPU'">
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>  
  ";

Get-ChildItem $dir *.csproj -recurse | 
   % { 
   $content = [xml](gc $_.FullName); 
   $project = $content.Project;
   $project
   $project.AppendChild($configs);
   # $content.Save($_.FullName);
   }

感谢您预先的支持!
1个回答

1

问题1:在PowerShell中,转义字符是`而不是引号。请记住,您还应该转义$符号

问题2:您遇到问题是因为$project.AppendChild();XmlNode,而您的$configs是XmlDocument

问题3:您可以这样做,但不确定MsBuild是否会满意

以下是脚本本身:

$dir = "C:\Work\scripttest\output\"
$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

$configs = [xml] "<PropertyGroup Condition=`"'`$(Configuration)|`$(Platform)' == 'Dev-1|AnyCPU'`">
     <OutputPath>bin\</OutputPath>
     <DefineConstants>TRACE</DefineConstants>
     <Optimize>true</Optimize>
     <DebugType>pdbonly</DebugType>
     <PlatformTarget>AnyCPU</PlatformTarget>
     <ErrorReport>prompt</ErrorReport>
</PropertyGroup>"

Get-ChildItem $dir *.csproj -recurse | 
% { 
  $content = [xml](gc $_.FullName); 
  $importNode = $content.ImportNode($configs.DocumentElement, $true) 
  $project = $content.Project;
  $project
  $project.AppendChild($importNode);
  # $content.Save($_.FullName);
}

正如您所看到的,我必须首先导入节点,因为它来自另一个文档。


如果csproj项目在TFS服务器上,则需要在修改之前进行签出,然后修改文件,最后进行签入。 - Kiquenet
@Kiquenet 网上有很多示例。以下是获取最新版本的方法:TF.exe get "$/Performance Testing/Project" /force /recursive - Andrey Marchuk
这会在我的文件中添加: <PropertyGroup xmlns=""> 这是无效的;我该如何让 'xmlns' 部分消失? - bc3tech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接