繁体   English   中英

用于从 CSV 文件修改 XML 的 Powershell 脚本

[英]Powershell script to modify XML from a CSV file

我正在尝试创建一个脚本 PowerShell 来修改 CSV 文件中 XML 文件节点的“密码”值。 每个帐户在 CSV 中都有不同的密码。

CSV 看起来像这样:

Computer;AccountName;Password
SERVER-01;service.testA;mdp123
SERVER-02;service.testB;mdp456
SERVER-03;service.testC;mdp789

XML 文件如下所示:

<Configuration>
    <TechnicalAccounts>
        <Account Id="service.testA">
            <Login>service.testA</Login>
            <Description>this is a description</Description>
            <Password>OldPassword</Password>
        </Account>
        <Account Id="service.testB">
            <Login>service.testB</Login>
            <Description>this is a description</Description>
            <Password>OldPassword</Password>
        </Account>
        <Account Id="service.testC">
            <Login>service.testC</Login>
            <Description>this is a description</Description>
            <Password>OldPassword</Password>
        </Account>
    </TechnicalAccounts>
</Configuration>

我试图将 CSV 中的所有数据放入一个选项卡中,然后如果“AccountName”与 XML 中的“Login”匹配,则它会更改关联的密码。 但我尝试过的一切都没有奏效。 你有什么想法? 谢谢您的帮助。

你没有在你的问题中展示你已经尝试过的东西(为了将来参考,这不是在 SO 上做事情的方式)。 但鉴于您的任务并不简单,您应该尝试以下操作:

假设您的 csv 已加载到$data而您的 xml 已加载到$xml ,那么:

$results = $data | ConvertFrom-Csv  -Delimiter ';'
#extract the relevant elements from the csv:
$results | ForEach-Object {
    $User = $_.AccountName 
    $Password = $_.Password 
}

#then use xpath to search within the xml for the target elements:
foreach($result in $results) {
    $target = $result.AccountName
    $exp = '//Account[./Login/text()="'+$target+'"]/Password'
    $node = $xml.SelectSingleNode($exp)
    $node.innerText=$result.Password
}

您可以将输出保存到一个新文件:

$xml.Save("mynewfile.xml")

经过研究和帮助,下面的代码对我有用。 我把它放在这里以防它可以帮助别人。

#=======================================================================
# Variables
#=======================================================================
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
$ScriptName = $MyInvocation.MyCommand.Name
$XMLFile    = "FILE.xml"
$CSVFile    = "FILE.csv"
$XMLPath    = "$ScriptPath\$XMLFile"
$CSVPath    = "$ScriptPath\$CSVFile"

#=======================================================================
# Import files
#=======================================================================
$InputData   = Get-Content -Path $CSVPath | ConvertFrom-Csv -Delimiter ";"
$XMLDocument = New-Object -TypeName System.Xml.XmlDocument
$XMLItem     = Get-Item -Path $XMLPath -ErrorAction Stop
$XMLDocument.Load($TechnicalAccountXmlItem.FullName)

#=======================================================================
# Updating XML
#=======================================================================
ForEach ($Data in $InputData)
{
    $Name = $Data.AccountName
    $Password = $Data.Password
    $XPath = '//Account[Login="{0}"]' -f $Name
    $AccountNode = $XMLDocument.SelectNodes($XPath)

    $AccountNode | ?{$_} | ForEach-Object {
        Write-Host ("The password of the account [{0}] has been modified from [{1}] to [{2}]." -f $_.Login,$_.Password,$Password)
        $_.Password = "$Password"
    }
}

$XMLDocument.Save("$ScriptPath\NewFile.xml")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM