且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Powershell将新的PropertyGroup添加到csproj

更新时间:2023-12-02 22:22:58

Q1:powershell中的转义字符为`,而不是引号。请记住,您还应该转义$符号

Q1: Escape character in powershell is ` and not quote. Keep in mind you should also escape $ symbol

第二季度:您遇到了问题,因为 $ project.AppendChild(); XmlNode ,而您的 $ configs 是XmlDocument

Q2: You had problems because $project.AppendChild(); is XmlNode and your $configs is XmlDocument

Q3 :您可以,但不确定MsBuild是否会满意

Q3: You can, but not sure if MsBuild will be happy with it

这是脚本本身:

$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);
}

如您所见,我不得不导入Node fisrt,因为它来自另一个文档

As you can see I had to ImportNode fisrt as it was coming from another document