Wednesday 14 May 2014

Update XML File using PowerShell

There are many blogs on internet already speaking about updating XML files in PowerShell, but I felt need of one consolidated blog where complex XML files can also be updated with long complex Hierarchy of XML nodes and attributes.
Below is an XML example which we will try in this blog to update at various level of node Hierarchy.
   1: <?xml version="1.0" encoding="utf-8"?>

   2: <Data version="2.0">

   3:   <Roles>

   4:     <Role Name="ManagementServer" Value="OldManagementServer" />

   5:   </Roles>

   6:   <SQL>

   7:     <Instance Server="OldSQLServer" Instance="MSSQLSERVER" Version="SQL Server 2012">

   8:       <Variable Name="SQLAdmin" Value="Domain\OldSQlAdmin" />

   9:       <Variable Name="SQLUser" Value="domain\sqluser" />

  10:     </Instance>

  11:   </SQL>

  12:   <VMs>

  13:     <VM Type="ClientVM">

  14:       <VMName>ClientVM</VMName>

  15:     </VM>

  16:     <VM Type="DNSServerVM">

  17:       <VMName>OldDNSServer</VMName>

  18:     </VM>

  19:   </VMs>

  20: </Data>

We will target to update Roles, Variables and VMName etc in this XML file. Below are the steps given separately on how we can update nodes and their attributes at various levels.
1. Define the variable which are required to be modified:-
   1: $path = 'C:\Users\sorastog\Desktop\blog\Variable.xml'

   2:  

   3: $ManagementServer = 'NewManagementServer'

   4: $SQLServer = 'NewSQLServer'

   5: $SQLAdmin = 'Domain\NewSQlAdmin'

   6: $DNSServerVMName = 'NewDNSServer'
2. Reading the content of XML file.
   1: $xml = [xml](Get-Content $path)
3.  Update ‘ManagementServer’: Changing Attribute value of node at level 3 based on ‘Name’ attribute on same level.
   1: $node = $xml.Data.Roles.Role | where {$_.Name -eq 'ManagementServer'}

   2: $node.Value = $ManagementServer
4. Update ‘SQLServer’: Changing Attribute value of node at level 3.
   1: $node = $xml.Data.SQL.Instance

   2: $node.Server = $SQLServer
5. Update ‘SQLAdmin’: Changing Attribute value of node at level 4 based on ‘Name’ attribute on same level.
   1: $node = $xml.Data.SQL.Instance.Variable | where {$_.Name -eq 'SQLAdmin'}

   2: $node.Value = $SQLAdmin
6. Update ‘DNSServerVM’: Changing Attribute value of node at level 4 based on ‘VMType’ attribute at above level.
   1: $node = $xml.Data.VMs.VM | where {$_.Type -eq 'DNSServerVM'}

   2: $node.VMName = $DNSServerVMName
7. Saving changes to XML file.
   1: $xml.Save($path)
The final PowerShell script would look like below:-
   1: $path = 'C:\Data.xml'

   2:  

   3: $ManagementServer = 'NewManagementServer'

   4: $SQLServer = 'NewSQLServer'

   5: $SQLAdmin = 'Domain\NewSQlAdmin'

   6: $DNSServerVMName = 'NewDNSServer'

   7:  

   8:  

   9: $xml = [xml](Get-Content $path)

  10:     

  11: $node = $xml.Data.Roles.Role | where {$_.Name -eq 'ManagementServer'}

  12: $node.Value = $ManagementServer

  13:     

  14: $node = $xml.Data.SQL.Instance

  15: $node.Server = $SQLServer

  16:  

  17: $node = $xml.Data.SQL.Instance.Variable | where {$_.Name -eq 'SQLAdmin'}

  18: $node.Value = $SQLAdmin

  19:  

  20: $node = $xml.Data.VMs.VM | where {$_.Type -eq 'DNSServerVM'}

  21: $node.VMName = $DNSServerVMName

  22:  

  23: $xml.Save($path)

Hope this will help you to update even complex XML files with multiple nodes and complex Hierarchy. If there are some XML nodes that you would like to update and the category is not included in this blog, please reply to this post and I will add it.
Till Then, Happy Scripting :)

Here is the Link to the updated Blog on MSDN.