3

I have the following ps1 file. There are three problems in this file.

Q1. How to use single quote and double-quote in XML? I googled and found that I need to put one extra single quote. I tried but didn't work.

Q2. I got "wrong type" error when I append new PropertyGroup as a child to Project node. How can I fix it.

Q3. Can I append multiple PropertyGroups to Project Node?

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

Thanks in advance!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Michael Sync
  • 4,834
  • 10
  • 40
  • 58

1 Answers1

1

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

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

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

And here's the script itself:

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

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

Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
  • any source code sample if csproj project is in TFS server. It would be required do Check out before modify it, and then modify file, and later do check in. – Kiquenet Oct 18 '12 at 10:14
  • @Kiquenet There are plenty samples on the net. Here's how you get latest version: TF.exe get "$/Performance Testing/Project" /force /recursive – Andrey Marchuk Oct 18 '12 at 11:00
  • This adds: `` to my file which is invalid; how do I get the 'xmlns' part to go away? – bc3tech May 18 '15 at 15:13