0

I'm starting with powershell and i need some help for deploy script which one is changing values in config file (xml). All settings values are in csv file :

ID,CountryCode,LanguageCode,SiteID,SiteName
00000001,1,us001,01,site1
00000002,1,us001,01.1,site1.1
00000003,2,fr002,02,site2
00000004,3,uk003,03,site3
00000005,4,de004,04,site4
00000006,4,de004,04.1,site4.1

ID is unique in local computer registry, created by deployed app

HKEY_LOCAL_MACHINE\SOFTWARE\EDITOR\APP ID reg_sz 00000001

Config xml for this app need to be updated, i'm trying to do it by powershell

<?xml version="1.0" encoding="utf-8"?>
<LocalSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <CountryCode>1</CountryCode>
 <LanguageCode>0001</LanguageCode>
 <SiteID>000000</SiteID>
 <SiteName>test</SiteName>

this is a part of powershell code

 $csv = Import-Csv 'C:\test\testvaluesFile.csv' | Select-Object "ID","CountryCode","LanguageCode","SiteID","SiteName"
 $XMLpath = ("C:\test\testconfigFile.xml")
 $XMLcontents = [XML] (Get-content $XMLpath)
 $nodes = $XMLcontents.SelectNodes("/LocalSetting/CountryCode","/LocalSetting/LanguageCode","/LocalSetting/SiteID","/LocalSetting/SiteName")

$ID = (Get-ItemProperty "HKLM:\SOFTWARE\EDITOR\APP").ID

foreach($row in $csv) {
  foreach($node in $nodes) {
    if ($ID -eq $row.ID) {
          $node.SetAttribute = $row.$nodes
        }
       }
      }


    $xml.Save("C:\test\testconfigFile.xml")

Thanks in advance

inChrg
  • 1
  • 2
  • This is a common ask on stackoverflow and many other Q&A sites. Just use the search box above to find similar posts here: [xml replace data](https://stackoverflow.com/search?q=xml+replace+data), or do the same vai a web search. [powershell relace xml data](https://www.bing.com/search?q=powershell%20relace%20xml%20data&qs=n&form=QBRE&sp=-1&pq=powershell%20relace%20xml%20data&sc=5-26&sk=&cvid=8393C35DEDCB4BB085FDC5E953706614) – postanote Apr 30 '20 at 20:37
  • Does this answer your question? [Change XML Element value with PowerShell](https://stackoverflow.com/questions/42699869/change-xml-element-value-with-powershell) – Alex_P May 01 '20 at 22:28

1 Answers1

0

This works

 $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

 $csv = Import-Csv '$scriptPath\testvaluesFile.csv'
 $XMLpath = ("$scriptPath\testconfigFile.xml")
 $XMLcontents = [XML] (Get-content $XMLpath)
 $node1 = $XMLcontents.SelectNodes("/CountryCode")
 $node2 = $XMLcontents.SelectNodes("/LanguageCode")
 $node3 = $XMLcontents.SelectNodes("/SiteID")
 $node4 = $XMLcontents.SelectNodes("SiteName")

$ID = (Get-ItemProperty "HKLM:\SOFTWARE\EDITOR\APP").ID

if($csvitem.ID -eq $CountryCode) {

        foreach($individual1 in $Node1)
        {
            $CountryCode = $csvitem.CountryCode
            $individual1.'#Text' = "$CountryCode"
        }
inChrg
  • 1
  • 2