【问题标题】:Powershell Script for finding and replacing values in an XML file matching values in CSV file as parameters用于在 XML 文件中查找和替换与 CSV 文件中的值匹配的值作为参数的 Powershell 脚本
【发布时间】:2020-05-07 15:53:50
【问题描述】:

我从 powershell 开始,我需要一些帮助来部署脚本,其中一个正在更改配置文件 (xml) 中的值。 所有设置值都在 csv 文件中:

ID、国家代码、语言代码、站点 ID、站点名称 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 在本地计算机注册表中是唯一的,由部署的应用程序创建

HKEY_LOCAL_MACHINE\SOFTWARE\EDITOR\APP ID reg_sz 00000001

此应用的配置 xml 需要更新,我正在尝试通过 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>

这是powershell代码的一部分

 $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")

提前致谢

【问题讨论】:

标签: xml powershell csv


【解决方案1】:

这行得通

 $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"
        }

【讨论】:

    猜你喜欢
    • 2013-10-11
    • 2012-09-13
    • 2017-08-07
    • 2021-11-06
    • 1970-01-01
    • 2018-03-07
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多