Sunday, May 19, 2024
HomePowershellReplace XML recordsdata utilizing PowerShell

Replace XML recordsdata utilizing PowerShell


There are various out there weblog posts on web explaining the best way to replace XML recordsdata in PowerShell. However I felt want of 1 consolidated weblog the place advanced XML recordsdata will be up to date utilizing PowerShell. These advanced xml recordsdata can have lengthy advanced hierarchy of XML nodes and attributes.

Allow us to attempt to replace beneath XML pattern at numerous ranges of node hierarchy.

Pattern XML Code

<?xml model="1.0" encoding="utf-8"?>
<Knowledge model="2.0">
  <Roles>
    <Function Identify="ManagementServer" Worth="OldManagementServer" />
  </Roles>
  <SQL>
    <Occasion Server="OldSQLServer" Occasion="MSSQLSERVER" Model="SQL Server 2012">
      <Variable Identify="SQLAdmin" Worth="DomainOldSQlAdmin" />
      <Variable Identify="SQLUser" Worth="domainsqluser" />
    </Occasion>
  </SQL>
  <VMs>
    <VM Kind="ClientVM">
      <VMName>ClientVM</VMName>
    </VM>
    <VM Kind="DNSServerVM">
      <VMName>OldDNSServer</VMName>
    </VM>
  </VMs>
</Knowledge>

Steps to observe

Save the above xml block in C: drive with title “Knowledge.xml”.

We are going to replace the nodes in XML file to make use of a brand new administration, SQL, and DNS servers. Beneath are the step-by-step PowerShell instructions on how we will replace the nodes and their attributes at numerous ranges.

  1. Outline the variables that we have to modify.

    $path="C:UserssorastogDesktopblogVariable.xml"
    $ManagementServer="NewManagementServer"
    $SQLServer="NewSQLServer"
    $SQLAdmin         = 'DomainNewSQlAdmin'
    $DNSServerVMName="NewDNSServer"
  2. Learn the content material of XML file.

    $xml = [xml](Get-Content material -Path $path)
  3. Replace ManagementServer: Change the attribute Worth of nodes at stage 3 based mostly on the Identify attribute on the identical stage.

    $node = $xml.Knowledge.Roles.Function | 
    the place {$_.Identify -eq 'ManagementServer'}
    $node.Worth = $ManagementServer
  4. Replace SQLServer: Change the attribute Worth of a node at stage 3.

    $node        = $xml.Knowledge.SQL.Occasion
    $node.Server = $SQLServer
  5. Replace SQLAdmin: Change the attribute Worth of nodes at stage 4 based mostly on the Identify attribute on the identical stage.

    $node = $xml.Knowledge.SQL.Occasion.Variable |
    the place {$_.Identify -eq 'SQLAdmin'}
    $node.Worth = $SQLAdmin
  6. Replace DNSServerVM: Change the attribute Worth of nodes at stage 4 based mostly on the VMType attribute on the stage above.

    $node = $xml.Knowledge.VMs.VM |
    the place {$_.Kind -eq 'DNSServerVM'}
    $node.VMName = $DNSServerVMName
  7. Save modifications to the XML file.

    $xml.Save($path)

Output

The ultimate PowerShell script will appear to be beneath:

$path="C:Knowledge.xml"
$ManagementServer="NewManagementServer"
$SQLServer="NewSQLServer"
$SQLAdmin         = 'DomainNewSQlAdmin'
$DNSServerVMName="NewDNSServer"

$xml = [xml](Get-Content material $path)

$node = $xml.Knowledge.Roles.Function |
    the place {$_.Identify -eq 'ManagementServer'}
$node.Worth = $ManagementServer

$node        = $xml.Knowledge.SQL.Occasion
$node.Server = $SQLServer

$node = $xml.Knowledge.SQL.Occasion.Variable |
    the place {$_.Identify -eq 'SQLAdmin'}
$node.Worth = $SQLAdmin

$node = $xml.Knowledge.VMs.VM |
    the place {$_.Kind -eq 'DNSServerVM'}
$node.VMName = $DNSServerVMName

$xml.Save($path)

The ultimate xml will appear to be beneath.

Image updated

Hope this submit will show you how to to replace advanced XML recordsdata utilizing PowerShell. If there are recommendations on the best way to enhance this weblog submit, then please remark beneath. I can be comfortable to incorporate them.

Until Then, Comfortable Scripting 🙂

Observe extra PowerShell posts right here.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments