Truemag

  • Home
  • Angular JS
  • C Sharp
  • DevOps




How to Update Xml using Powershell?

Powershell is one of the scripting and configuration management tools that is being widely used for automation on the windows platform. Powershell runs on Windows, Linux as well as macOS.

Some of the features of PowerShell are

  1. Pipeline the commands
  2. Desired State Configuration(DSC)

We can use PowerShell for performing a variety of tasks such as creating a text file, updating them and many more.

One of the basic requirements in PowerShell is to read an XML file and update the contents and save the changes in the XML file.

Below is the code that can be used to achieve it.

Sample XML

<?xml version="1.0" encoding="utf-8"?>
<Data version="2.0">
  <Roles>
    <Role Name="ManagementServer" Value="server" />
    <Role Name="ADServer" Value="NewADServer" />
    <Role Name="ADServer" Value="NewADServer" />
  </Roles>
  <SQL>
    <Instance Server="NewSQLServer" Instance="MSSQLSERVER" Version="SQL Server 2012">
      <Variable Name="SQLAdmin" Value="xyz\xyz" />
      <Variable Name="SQLUser" Value="domain\sqluser" />
    </Instance>
  </SQL>
  <VMs>
    <VM Type="ClientVM">
      <VMName>ClientVM</VMName>
    </VM>
    <VM Type="DNSServerVM">
      <VMName>NewDNSServer</VMName>
    </VM>
  </VMs>
  <Course>
    <Subject>History</Subject>
    <Subject>Computers</Subject>
  </Course>
  <AllContacts>
    <Contact>
      <ContactType Type="Mobile" />
      <Details Number="000000000" />
    </Contact>
    <Contact>
      <ContactType Type="Landline" />
      <Details Number="11111111111" />
    </Contact>
  </AllContacts>
</Data>

Below is the Powershell script to update the role and course:
[insertAdsense]

$path = 'D:\test.xml' 
$ManagementServer='iris-csg-731'
$SqlAdmin ='admin/admin'
$UpdateContactValue = '123456'
$xml =  (Get-Content $path)
$serverName = $xml.Data.Roles.Role | where {$_.Name -eq 'ManagementServer'}
$sqlAdminNode = $xml.Data.SQL.Instance.Variable | where {$_.Name -eq 'SQLAdmin'}
$serverName.Value = $ManagementServer
$sqlAdminNode.Value = $SqlAdmin
$xml.Save($path)
Mar 18, 2018Prashant Kumar
How to add Sticky footer in Materialize design?New features in Angular 6
  • Case Statement in Azure DevOps
  • How to solve file not found error in Linux?
  • How to delete local branch in git?
  • What is the git command to move branch pointer to different commit without checkout ?
  • What command tracks the revisions in git ?

Sign up for our newsletter to receive the latest news and event postings.

Recent Posts
  • Case Statement in Azure DevOps
  • How to solve file not found error in Linux?
  • How to delete local branch in git?
  • What is the git command to move branch pointer to different commit without checkout ?
  • What command tracks the revisions in git ?
Recent Comments
  • How to change directory in git bash? - Prashant's Blog on How to delete local branch in git?
  • eebest8 on How to use Http Post in Angular 4

  • Case Statement in Azure DevOps
  • How to solve file not found error in Linux?
  • How to delete local branch in git?
  • What is the git command to move branch pointer to different commit without checkout ?
  • What command tracks the revisions in git ?
  • Home
  • Angular JS
  • C Sharp
  • DevOps
  • June 2020
  • April 2020
  • February 2020
  • July 2019
  • June 2019
  • May 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • July 2018
  • March 2018
  • February 2018
2018 © Prashant's Blog