0

Please help me out with a PowerShell script for:

  1. Deleting a specific <Section Name="APILibraries"> node in Config-B.xml.

  2. Reading a specific <Section Name="APILibraries"> node from Config-A.xml.

  3. Appending in config-B.xml with step 2 out value.

Code Block 1 & 2 are working fine. I'm able to read the value as well as delete the node from config-B.xml. However, in code block 3 the second-last line of the PowerShell script is throwing error like it can't import null node.

Here is the the Config-A.xml which is source and Config-B.xml is the target along with the PowerShell script.

Config-A.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <Data>
    <Section Name="APILibraries">
      <Item Name="TIMSS.API.User" Layer="User" Type="Custom" RootNamespace="TIMSS.API.User" FileName="TIMSS.API.User.dll" />
      <Item Name="TIMSS.API.User.Generated" Layer="User" Type="Generated" RootNamespace="TIMSS.API.User" FileName="Personify.API.User.Generated.dll" />
      <Item Name="TIMSS.API.Base" Layer="Base" Type="Custom" RootNamespace="TIMSS.API.Base" FileName="TIMSS.API.Base.dll" />
      <Item Name="TIMSS.API.Generated" Layer="Base" Type="Generated" RootNamespace="TIMSS.API.Generated" FileName="TIMSS.API.Generated.dll" />
      <Item Name="TIMSS.API.Core" Layer="Core" Type="Custom" RootNamespace="TIMSS.API.Core" FileName="TIMSS.API.Core.dll" />
    </Section>
  </Data>
</Configuration>

Config-B.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <Data>
    <Section Name="APILibraries">
      <Item Name="TIMSS.API.Base" Layer="Base" Type="Custom" RootNamespace="TIMSS.API.Base" FileName="TIMSS.API.Base.dll" />
      <Item Name="TIMSS.API.Generated" Layer="Base" Type="Generated" RootNamespace="TIMSS.API.Generated" FileName="TIMSS.API.Generated.dll" />
      <Item Name="TIMSS.API.Core" Layer="Core" Type="Custom" RootNamespace="TIMSS.API.Core" FileName="TIMSS.API.Core.dll" />
    </Section>
  </Data>
</Configuration>

PowerShell script:

###Code Block - 1 : Get Source XML <Section Name="APILibraries"> Node Value ###
$CustomWinClientConfigXmlSource = "SourcePath\Config.xml"

[xml]$SourceConfigXml = Get-Content -Path "$CustomWinClientConfigXmlSource" -Raw
$SourceXmlNode = $SourceConfigXml | Select-Xml -XPath "//Section[@Name='APILibraries']"
$SourceXmlOutput = Write-Output "$SourceXmlNode"
$SourceXMLNodeValue = "$SourceXmlOutput"


### Code Block - 2 : Get The Target XML <Section Name="APILibraries"> Node Value And Delete It ##
$WinClientConfigFiles = "Config.xml"
$CustomWinClientConfigXmlTarget = "TargetPath\$WinClientConfigFiles"
$Path = "$CustomWinClientConfigXmlTarget"

[Xml]$servicefactoryconfig = Get-Content -Path $Path -Raw
$old = $servicefactoryconfig.SelectSingleNode("/Configuration/Data/Section[@Name='APILibraries']")
$parent = $old.ParentNode
[void] $parent.RemoveChild($old)


### Code Block - 3 : Append The Target XML <Section Name="APILibraries"> With Source XML Node Value ##
try {
    $newNode = [Xml] @"
$SourceXMLNodeValue
"@
} catch {
    Write-Error -Message 'Ignoring The Error Message' -ErrorAction Ignore
}

[void] $parent.AppendChild($servicefactoryconfig.ImportNode($newNode.DocumentElement, $true))

$servicefactoryconfig.Save($path)
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Possible duplicate of [Powershell XML importnode from different file](https://stackoverflow.com/questions/9944885/powershell-xml-importnode-from-different-file) – Ansgar Wiechers May 30 '18 at 08:01
  • appreciate for your help. If possible to re-modify the existing one than i would i need to do it. Can you plz help on this - @AnsgarWiechers – VISINGH10688 May 30 '18 at 08:20
  • 2
    Instead of mangling XML objects into strings and then recreating XML objects from those strings just import the node you originally selected from the first XML: `[void] $parent.AppendChild($servicefactoryconfig.ImportNode($SourceXmlNode.Node, $true))`. – Ansgar Wiechers May 30 '18 at 08:26
  • Thanks For the Help. It works for me.However one more help, what if i would like to put same node at certain position in Config-B.xml file.For example I would like to paste it over on second position like :
    – VISINGH10688 May 30 '18 at 09:42
  • Thanks for your geat help. It is working for me now. I have re-modified the code with this one :[void] $parent.SelectSingleNode("/Configuration/Data/Section[@Name='Settings']").AppendChild($servicefactoryconfig.ImportNode($SourceXmlNode.Node, $true)) – VISINGH10688 May 30 '18 at 10:25

0 Answers0