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
- Pipeline the commands
- 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)
Recent Comments